jQuery - 对div内容进行排序

时间:2012-07-11 13:36:08

标签: jquery

  

可能重复:
  jQuery: Sort div's according to content of different sub divs

是否可以对div的内容进行排序

<div id="sort">
    <div id="something">A2</div>
    <div id="something">A1</div>
    <div id="something">A4</div>
    <div id="something">A3</div>
</div>

排序到:

<div id="sort">
    <div id="something">A1</div>
    <div id="something">A2</div>
    <div id="something">A3</div>
    <div id="something">A4</div>
</div>

这样的事情????

$('#sort').sort();

1 个答案:

答案 0 :(得分:15)

这个怎么样?

var mylist = $('#sort');

var listitems = mylist.children('div').get();

listitems.sort(function(a, b) {
   return $(a).text().toUpperCase().localeCompare($(b).text().toUpperCase());
});

$.each(listitems, function(index, item) {
   mylist.append(item); 
});

来自:http://www.onemoretake.com/2009/02/25/sorting-elements-with-jquery/

而且:How may I sort a list alphabetically using jQuery?