有人能告诉我如何在 3 列中输出这个对象列表并按字母顺序对列表进行排序吗?我试过else if
但是...
CSS:
.main {
float:left;
width:150px;
background:#9c9;
}
.center {
float:left;
width:150px;
background:#c9c;
}
HTML:
<head>
<script type="text/javascript">
var data = [
{name: "Dan", age: 25, desc: "test"},
{name: "Aary", age: 15, desc: "description"},
{name: "Bary", age: 15, desc: "description"},
{name: "Cary", age: 15, desc: "description"},
{name: "Dary", age: 15, desc: "description"},
{name: "Fary", age: 15, desc: "description"},
{name: "Tom", age: 18, desc: "haaha"}
];
function findName(personName){
return $.grep(data, function(item){
return item.name == personName;
});
}
function details(personName)
{
var person = findName(personName.toString())[0];
$('#description').html(person.desc);
}
</script>
</head>
<body>
<div>
<div class='main'>
</div>
<div class='center'>
</div>
<div id="description">
</div>
</div>
</body>
JS:
$(function () {
for (var i = 0; i < data.length; i++) {
if (i % 2 == 0) {
$('.main').append("<a onclick='javascript:details(\"" + data[i].name + "\")'>" + data[i].name + " <strong>(" + data[i].age + ")</strong>" + "</a></br>");
} else {
$('.center').append("<a onclick='javascript:details(\"" + data[i].name + "\")'>" + data[i].name + " <strong>(" + data[i].age + ")</strong>" + "</a></br>");
}
}
});