我正在使用以下代码:
$('.game-list-right-bottom > .game-list-file').each(function(i) {
if( i % 4 == 0 ) {
$(this).nextAll().andSelf().slice(0,4).wrapAll('<div class="game-list-files"></div>');
}
});
转过来:
<div class="game-list-right-bottom">
<div class="game-list-file"></div>
<div class="game-list-file"></div>
<div class="game-list-file"></div>
<div class="game-list-file"></div>
<div class="game-list-file"></div>
<div class="game-list-file"></div>
</div>
进入这个:
<div class="game-list-right-bottom">
<div class="game-list-files">
<div class="game-list-file"></div>
<div class="game-list-file"></div>
<div class="game-list-file"></div>
<div class="game-list-file"></div>
</div>
<div class="game-list-files">
<div class="game-list-file"></div>
<div class="game-list-file"></div>
</div>
</div>
(基本上将.game-list-files
包装到每四个.game-list-file中。
问题是我有多个.game-list-right-bottom
:
<div class="game-list-right-bottom">
<div class="game-list-file"></div>
<div class="game-list-file"></div>
<div class="game-list-file"></div>
</div>
<div class="game-list-right-bottom">
<div class="game-list-file"></div>
<div class="game-list-file"></div>
<div class="game-list-file"></div>
<div class="game-list-file"></div>
<div class="game-list-file"></div>
<div class="game-list-file"></div>
</div>
所以最终会这样:
<div class="game-list-right-bottom">
<div class="game-list-file"></div>
<div class="game-list-file"></div>
<div class="game-list-file"></div>
</div>
<div class="game-list-right-bottom">
<div class="game-list-file"></div>
<div class="game-list-files">
<div class="game-list-file"></div>
</div>
<div class="game-list-file"></div>
<div class="game-list-file"></div>
<div class="game-list-file"></div>
<div class="game-list-file"></div>
</div>
我想将jQuery代码单独应用于每个.game-list-right-bottom
。
如何存档?
答案 0 :(得分:2)
首先使用.each()
.game-list-right-bottom
,而不是孩子。
答案 1 :(得分:2)
试试这个
$('.game-list-right-bottom').each(function () {
$(this).children('.game-list-file').each(function(i) {
if( i % 4 == 0 ) {
$(this).nextAll().andSelf().slice(0,4).wrapAll('<div class="game-list-files"></div>');
}
});
});