0

我正在使用以下代码:

$('.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. 如何存档?

4

2 回答 2

2

从使用.each()for your开始.game-list-right-bottom,而不是 children。

于 2013-05-13T04:11:46.547 回答
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>');
      }
    });
  });
于 2013-05-13T04:24:09.100 回答