日期:2014-05-16 浏览次数:20368 次
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script src="jquery-1.2.6.pack.js"></script>
<style>
ul,li,dl,dd{padding:0;margin:0;}
ul{width:410px;}
li{list-style:none;border:1px solid red;float:left;width:410px;}
dd{float:left;width:100px;line-height:20px;border:1px solid #ddd;padding:3px 0;}
.cur{background-color:#ccc;}
</style>
</head>
<body>
<ul id="container"></ul>
<script>
var json=[
{id:"000002",name:"万科A",pingyin:"WKA",city:"深圳"}
,{id:"000003",name:"万科A",pingyin:"WKA",city:"深圳"}
,{id:"000004",name:"万科A",pingyin:"WKA",city:"深圳"}
,{id:"000005",name:"万科A",pingyin:"WKA",city:"深圳"}
,{id:"000006",name:"万科A",pingyin:"WKA",city:"深圳"}
];
$(function(){
json.forEach(function(i){
$("#container").append("<li><dl><dd>"+ i.id +"</dd><dd>"+ i.name +"</dd><dd>"+ i.pingyin +"</dd><dd>"+ i.city +"</dd></dl></li>");
});
$("#container li").eq(0).addClass("cur");
$(document).keydown(function(event){
var cur=$("#container li.cur");
var length=$("#container li").length;
var first=$("#container li").eq(0);
var last=$("#container li").eq(length-1);
if(event.keyCode==40)
{
if(cur.html()!=last.html())
{
cur.removeClass("cur");
cur.next().addClass('cur');
}
}
if(event.keyCode==38)
{
if(cur.html()!=first.html())
{
cur.removeClass("cur");
cur.prev().addClass('cur');
}
}
});
});
</script>
</body>
</html>