我正在尝试按注释ID对.depth-3
元素进行排序,并按升序显示它们。 ul
类.children
正在重复并且也在所有深度中使用,但只需要对.depth-3
个元素进行排序(因为其中一些元素是对彼此的回复,因此不会按升序显示)。
外观如何:
<ol class="commentlist">
<li id="comment-677" class="comment byuser comment-author-admin even thread-even depth-1">
<div id="comment-677">
<div class="comment-content">comment-677
</div>
</div>
<ul class="children">
<li id="comment-678" class="comment odd alt depth-2">
<div id="comment-678">
<div class="comment-content">comment-678
</div>
</div>
<ul class="children">
<li id="comment-680" class="comment byuser comment-author-admin even depth-3" data-value="680">
<div id="comment-680">
<div class="comment-content">comment-680
</div>
</div>
</li>
<li id="comment-686" class="comment byuser comment-author-admin odd alt depth-3" data-value="686">
<div id="comment-686">
<div class="comment-content">comment-686
</div>
</div>
</li>
<li id="comment-688" class="comment byuser comment-author-admin even depth-3" data-value="688">
<div id="comment-688">
<div class="comment-content">comment-688
</div>
</div>
</li>
<li id="comment-687" class="comment byuser comment-author-admin odd alt depth-3" data-value="687">
<div id="comment-687">
<div class="comment-content">comment-687
</div>
</div>
</li>
<li id="comment-689" class="comment byuser comment-author-admin even depth-3" data-value="689">
<div id="comment-689">
<div class="comment-content">comment-689
</div>
</div>
</li>
<li id="comment-684" class="comment byuser comment-author-admin odd alt depth-3" data-value="684">
<div id="comment-684">
<div class="comment-content">comment-684
</div>
</div>
</li>
</ul>
</li>
<li id="li-comment-681" class="comment byuser comment-author-admin even depth-2">
<div id="comment-681">
<div class="comment-content">comment-681
</div>
</div>
<ul class="children">
<li id="li-comment-682" class="comment byuser comment-author-admin odd alt depth-3" data-value="682">
<div id="comment-682">
<div class="comment-content">comment-682
</div>
</div>
</li>
<li id="li-comment-690" class="comment byuser comment-author-admin even depth-3" data-value="690">
<div id="comment-690">
<div class="comment-content">comment-690
</div>
</div>
</li>
<li id="li-comment-683" class="comment byuser comment-author-admin odd alt depth-3" data-value="683">
<div id="comment-683">
<div class="comment-content">comment-683
</div>
</div>
</li>
<li id="li-comment-685" class="comment byuser comment-author-admin even depth-3" data-value="685">
<div id="comment-685">
<div class="comment-content">comment-685
</div>
</div>
</li>
<li id="li-comment-691" class="comment byuser comment-author-admin odd alt depth-3" data-value="691">
<div id="comment-691">
<div class="comment-content">comment-691
</div>
</div>
</li>
</ul>
</li>
</ul>
<li id="li-comment-679" class="comment byuser comment-author-admin odd alt thread-odd thread-alt depth-1">
<div id="comment-679">
<div class="comment-content">comment-679
</div>
</div>
<ul class="children">
<li id="li-comment-692" class="comment byuser comment-author-admin even depth-2">
<div id="comment-691">
<div class="comment-content">comment-691
</div>
</div>
</li>
</ul>
</li>
我希望它看起来如何:
<ol class="commentlist">
<li id="677" class="comment byuser comment-author-admin even thread-even depth-1">
<div id="comment-677">
<div class="comment-content">comment-677
</div>
</div>
<ul class="children">
<li id="comment-678" class="comment odd alt depth-2">
<div id="comment-678">
<div class="comment-content">comment-678
</div>
</div>
<ul class="children">
<li id="680" class="comment byuser comment-author-admin even depth-3" data-value="680">
<div id="comment-680">
<div class="comment-content">comment-680
</div>
</div>
</li>
<li id="comment-684" class="comment byuser comment-author-admin even depth-3" data-value="684">
<div id="comment-684">
<div class="comment-content">comment-684
</div>
</div>
</li>
<li id="comment-686" class="comment byuser comment-author-admin odd alt depth-3" data-value="686">
<div id="comment-686">
<div class="comment-content">comment-686
</div>
</div>
</li>
</ul>
</li>
<li id="li-comment-681" class="comment byuser comment-author-admin even depth-2">
<div id="comment-681">
<div class="comment-content">comment-681
</div>
</div>
<ul class="children">
<li id="li-comment-682" class="comment byuser comment-author-admin odd alt depth-3" data-value="682">
<div id="comment-682">
<div class="comment-content">comment-682
</div>
</div>
</li>
<li id="li-comment-683" class="comment byuser comment-author-admin odd alt depth-3" data-value="683">
<div id="comment-683">
<div class="comment-content">comment-683
</div>
</div>
</li>
<li id="li-comment-690" class="comment byuser comment-author-admin even depth-3" data-value="690">
<div id="comment-690">
<div class="comment-content">comment-690
</div>
</div>
</li>
</ul>
</li>
</ul>
<li id="li-comment-679" class="comment byuser comment-author-admin odd alt thread-odd thread-alt depth-1">
<div id="comment-679">
<div class="comment-content">comment-679
</div>
</div>
<ul class="children">
<li id="li-comment-692" class="comment byuser comment-author-admin even depth-2">
<div id="comment-691">
<div class="comment-content">comment-691
</div>
</div>
</li>
</ul>
</li>
我的尝试,它获得了正确的顺序,但所有元素都在每个ul
var $wrapper = $( '.children' );
$wrapper.find('.depth-3').sort(function (a, b) {
return +a.dataset.value - +b.dataset.value;
})
.appendTo( $wrapper );
这是fiddle
答案 0 :(得分:1)
$(".depth-2").each(function(){
var children = $(this).find(".depth-3").sort(function (a, b) {
return +a.dataset.value - +b.dataset.value;
});
$(this).children(".children").empty().append(children);
});