如何在JQuery中使用ul标签对li标签进行排序?

时间:2014-02-10 07:36:39

标签: javascript jquery sorting html-lists

<ul id ="sort">
    <li id = '2014-02-07'>tralala<li>
    <li id = '2013-02-09'>tralala<li>
    <li id = '2014-01-04'>tralala<li>
    <li id = '2011-09-05'>tralala<li>
</ul>

我希望结果是:

<ul id ="sort">
    <li id = '2014-02-07'>tralala<li>
    <li id = '2014-01-04'>tralala<li>
    <li id = '2013-02-09'>tralala<li>
    <li id = '2011-09-05'>tralala<li>
</ul>

我想在jQuery或Javascript中使用id对desc li进行排序。谢谢

1 个答案:

答案 0 :(得分:1)

你可以这样做:

var list = $('#sort'),
    items = list.children().get();
list.append(items.sort(function(a, b){
    return new Date(b.id) - new Date(a.id);
}));

以下是演示:http://jsfiddle.net/wared/EE5HB/