如何按照span标签和父标签进行排序跟随他(jquery)

时间:2014-12-05 15:15:38

标签: jquery html

我想这样排序:http://jsfiddle.net/chepe263/2SXN9/ 但只有span已经排序,<li>标记不会跟<span>标记

$(function(){
var elem = $('‪#‎narrow‬-by-list').find('li.years').sort(sortMe);
function sortMe(a, b) {
return a.className > b.className;
}
$('#narrow-by-list li.month').parent().append(elem);
});
$(function () {
var liContents = [];
$('#narrow-by-list li.month span, #narrow-by-list li.years span').each (function () {
liContents.push(parseInt($(this).text(), 10));
});
liContents.sort(numOrdDesc);
$('#narrow-by-list li.month span, #narrow-by-list li.years span').each (function () {
$(this).text(liContents.pop());
});
});
function numOrdDesc(a, b){ return (b-a); }
<dd>
<ol>
<li rel="0-3 months" class="month">
<a class="amshopby-attr" href="#">0-3 months</a> (3)<span>3</span></li>
<li rel="3-6 months" class="month">
<a class="amshopby-attr" href="#">3-6 months</a> (2)<span>36</span></li>
<li rel="6-9 months" class="month">
<a class="amshopby-attr" href="#">6-9 months</a> (2)<span>69</span></li>
<li rel="9-12 months" class="month">
<a class="amshopby-attr" href="#">9-12 months</a> (1)<span>912</span></li>
<li rel="14-16 years" class="years">
<a class="amshopby-attr" href="#">14-16 years</a> (1)<span>1416</span></li>
<li rel="16+ years" class="years">
<a class="amshopby-attr" href="#">16+ years</a> (1)<span>1600</span></li>
<li rel="2-3 years" class="years">
<a class="amshopby-attr" href="#">2-3 years</a> (5)<span>23</span></li>
<li rel="4-5 years" class="years">
<a class="amshopby-attr" href="#">4-5 years</a> (3)<span>45</span></li>
<li rel="6-7 years" class="years">
<a class="amshopby-attr" href="#">6-7 years</a> (2)<span>67</span></li></ol>
</dd>

我希望李小姐能够顺利度过。

1 个答案:

答案 0 :(得分:1)

请参阅:http://jsfiddle.net/2SXN9/14/

$(function () {
    var liContents = [];
    $('span').each (function () {
        liContents.push(
            { 

                label : $(this).parent().html(),
                value : parseInt($(this).text(), 10)
            });
    });

    liContents.sort(numOrdDesc);

    $('li').each (function () {
        $(this).html(liContents.pop().label);
    });
});

function numOrdDesc(a, b){ return (b.value-a.value); }