开发者

jQuery arrange li order base on #ID

开发者 https://www.devze.com 2022-12-27 18:02 出处:网络
I have the following code: <ul> <li id=\"project_25\">Proj Something</li> <li id=\"project_26\">Proj Blah</li>

I have the following code:

<ul>
<li id="project_25">Proj Something</li>
<li id="project_26">Proj Blah</li>
<li id="project_27">Proj Else</li>
<li id="project_31">Proj More</li>
...
</ul>

Is there a way to arrange these LIs, reverse their order, based on the LI ID?

Something like:

<ul>
<li id="project_31">Proj More</li>
<li id="project_27">Proj Else</li>
<li id="pro开发者_JAVA百科ject_26">Proj Blah</li>
<li id="project_25">Proj Something</li>
...
</ul>

Thank you.


var arr = $('li').get();    
arr.reverse();

$.each(arr, function(i,v){
    $('ul').append(this);
});


Please try the code given below:

var mylist = $('ul');
var listitems = mylist.children('li').get();
listitems.sort(function(a, b) {
   var compA = $(a).text().toUpperCase();
   var compB = $(b).text().toUpperCase();
   return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
})
$.each(listitems, function(idx, itm) { mylist.append(itm); });

I found this here.

HTH

0

精彩评论

暂无评论...
验证码 换一张
取 消