0

コンテナーとして使用する div がいくつかあります。それらをループしてから、その中のアイテムをループします。

そうではなく$('.stackContainer .stackItem').each(、次のようなものです:

// setup stacks
$('.stackContainer').each(function(containerIndex) {
    //console.log($(this));
    console.log(containerIndex);

    $(this).('.stackItem').each(function(itemIndex) {
        console.log(itemIndex);     
    }
});

その時だけ働く。これはどのように可能ですか?

4

4 に答える 4

2

試す

$('.stackContainer').each(function(containerIndex) {
    //console.log($(this));
    console.log(containerIndex);

    $(this).find('.stackItem').each(function(itemIndex) {
        console.log(itemIndex);     
    }
});
于 2012-06-10T12:19:12.380 に答える
1

find()メソッドを試してください:

$('.stackContainer').each(function(containerIndex) {
    //console.log($(this));
    console.log(containerIndex);

    $(this).find('.stackItem').each(function(itemIndex) {
        console.log(itemIndex);     
    }
});
于 2012-06-10T12:18:46.763 に答える
0

他の回答が示しているように、これを行う多くの方法。これが1つの解決策です:http://jsfiddle.net/cezHH/3/

$(".stackContainer").each(function(stackIndex) {
    $(this).children().css('class', '.stackItem').each(function(itemIndex) {
        $(this).html($(this).html() + "=>" + itemIndex);
    });
});​

または、ページに表示される順序ですべての.stackItemdiv を反復処理するだけで、実際には親 div を気にしない場合は、次の.stackContainerようにすることができます。$('.stackItem').each(function(index) { ... });

.stackItemdivを反復するループをネストする理由は、itemIndex親コンテナーを切り替えるたびに変数をゼロにリセットする場合です。

于 2012-06-10T12:34:05.267 に答える
0

ちなみに、パフォーマンス上の理由から、コレクションが任意のサイズになる可能性がある場合は、jQuery の each() の使用を避ける必要があります。

http://net.tutsplus.com/tutorials/javascript-ajax/10-ways-to-instantly-increase-your-jquery-performance/

http://jsperf.com/jquery-each-vs-for-loop

于 2012-06-10T13:08:36.600 に答える