是否可以使用data函数对div进行排序?
html
<div id="gp_21" class="line">one</div>
<div id="gp_35" class="line">two</div>
<div id="gp_11" class="line">three</div>
<div id="gp_29" class="line">four</div>
<hr />
<div id="check"></div>
js
$(document).ready(function rt() {
$('#gp_21').data("rtt", { age: '251351' });
$('#gp_35').data("rtt", { age: '25131151' });
$('#gp_11').data("rtt", { age: '251' });
$('#gp_29').data("rtt", { age: '25131148' });
check();
});
function check()
{
$('.line').each(function() {
age = $('#'+this.id+'').data("rtt").age;
$('#check').append('-> '+age+' - '+this.id+'<br />');
});
}
工作示例 -> http://www.jsfiddle.net/V9Euk/265/
提前致谢! 彼得
最佳答案
嗨看到这个http://www.jsfiddle.net/V9Euk/268/
function check()
{
var ages= [];
var ids = []
$('.line').each(function() {
ages[ages.length] = $('#'+this.id+'').data("rtt").age;
ids[ids.length] = this.id;
});
ages.sort(sortByAge);
$(ages).each(function(i, v) {
for(key in ids)
{
if($("#"+ids[key]).data("rtt").age == v)
{
$('#check').append('-> '+v+' - '+ids[key]+'<br />');
}
}
});
}
function sortByAge(a, b)
{
return a-b;
}
您好,我更新了附加排序 div 的答案。
关于javascript - Jquery - 使用 data() 按类对 div 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3583367/