Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery。按字母顺序排列名单_Jquery - Fatal编程技术网

Jquery。按字母顺序排列名单

Jquery。按字母顺序排列名单,jquery,Jquery,我在一页上找到了一张名单(名字和姓氏)。我希望用户能够使用jQuery(或普通javascript)在firstname或lastname上对该列表进行排序。如何做到这一点 <ul> <li>Michael Scott</li> <li>Jonathan Torry</li> </ul> 迈克尔·斯科特 乔纳森·托里 非常感谢所有的投入 Array.sort()是您所需要的 var items = []; $('ul

我在一页上找到了一张名单(名字和姓氏)。我希望用户能够使用jQuery(或普通javascript)在firstname或lastname上对该列表进行排序。如何做到这一点

<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();而不是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