【发布时间】:2015-11-16 21:30:17
【问题描述】:
我正在尝试使用 jQuery 对 div 进行排序,并且我有这个 html:
<div id="container">
<button type="button" id="sort">sort</button>
<div class="entry" id="lala">
<a target="_blank" href="http://lala">lala</a>
<span class="times">4</span>
</div>
<div class="entry" id="lele">
<a target="_blank" href="http://lele.com">lele.com</a>
<span class="times">1</span>
</div>
<div class="entry" id="lolo">
<a target="_blank" href="http://lolo">lolo.com</a>
<span class="times">2</span>
</div>
</div>
我想使用类“times”值对“entry”div 进行排序。我的 javascript 函数不起作用,因为a.child is undefined:
$( document ).ready(function() {
$( "#add" ).click(function() {
var elems = $('#container').children('.entry').remove();
elems.sort(function(a,b){
var valueA = parseInt(a.children(".times").text());
var valueB = parseInt(b.children(".times").text());
return (valueA < valueB) ? -1 : (valueA > valueB) ? 1 : 0;
});
$('#container').append(elems);
});
});
谢谢 ;)
【问题讨论】:
标签: javascript jquery sorting