Jquery。按字母顺序排列名单
我在一页上找到了一张名单(名字和姓氏)。我希望用户能够使用jQuery(或普通javascript)在firstname或lastname上对该列表进行排序。如何做到这一点Jquery。按字母顺序排列名单,jquery,Jquery,我在一页上找到了一张名单(名字和姓氏)。我希望用户能够使用jQuery(或普通javascript)在firstname或lastname上对该列表进行排序。如何做到这一点 <ul> <li>Michael Scott</li> <li>Jonathan Torry</li> </ul> 迈克尔·斯科特 乔纳森·托里 非常感谢所有的投入 Array.sort()是您所需要的 var items = []; $('ul
<ul>
<li>Michael Scott</li>
<li>Jonathan Torry</li>
</ul>
- 迈克尔·斯科特
- 乔纳森·托里
非常感谢所有的投入 Array.sort()是您所需要的
var items = [];
$('ul li').each(function(){
items.push($(this).html());
})
items.sort();
示例:试试我的插件
要订购fistname和lastname,请按以下方式更改列表:
<ul id="ul1">
<li><span>Michael</span> <span>Scott</span></li>
<li><span>Jonathan</span> <span>Torry</span></li>
</ul>
按姓氏订购
$('#ul1').listorder( { child:'li', childValue:function(){return $(this.children([1]).text();} });
$(this.children([0]) select the 1st span
$(this.children([1]) select the 2nd span
看起来很有趣。我怎样才能改变名字的顺序?升序和降序?您可以简单地调用
items.reverse()要反转ititems.reverse()的数组上的代码>;而不是items.sort();没有更改顺序。不,我的意思是在调用items.sort()
之后必须调用items.reverse()
。Sort将首先对其进行排序,reverse将简单地给出相反的顺序。那有用吗?
$('#ul1').listorder( { child:'li', childValue:function(){return $(this.children([1]).text();} });
$(this.children([0]) select the 1st span
$(this.children([1]) select the 2nd span