2014-12-05 58 views
0

我想排序是這樣的:http://jsfiddle.net/chepe263/2SXN9/ 但只有跨度已排序,<li>標籤不遵循<span>標籤如何從跨度標籤和父標籤排序跟着跟着他(jQuery的)

$(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>

我希望li a遵循跨度謝謝。

回答

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); }