0
$("#questions .question").each(function() {}

.questionli の内部には.question、 class を持つ別のリストがあり.answers、それぞれに class の li があり.answerます。

上記のループ内でそれらをターゲットにするためにこれを試しましたが、うまくいきませんでした:

$(this).$(".answer").each(function() {}

これを使用して、ループ内で各回答をターゲットにする方法を知っている人はいますか?

4

5 に答える 5

3

$(this).find('.answer')また$('.answer', this)

それらは同等です。jQuery ソースから:

// HANDLE: $(expr, context)
// (which is just equivalent to: $(context).find(expr)
} else {
    return this.constructor( context ).find( selector );
}
于 2013-03-06T13:40:54.247 に答える
0

使用$(this):

$("#questions .question").each(function() {
    console.log($(this)); // each .question li
}
于 2013-03-06T13:41:17.890 に答える
0
$("#questions .question").each(function() {
    $(this).find(".answer").each( function() {
        //code here
    } );
}
于 2013-03-06T13:41:35.057 に答える
0
$("#questions .question").each(function(index, element) {
    $(element).find('.answer').each(function(index, element) {
        /* do stuff */
    });
});

http://api.jquery.com/jQuery.each/

于 2013-03-06T13:46:11.593 に答える
0

セレクターを適用するコンテキストを渡すことができます。この場合、渡すthisと目的が達成されます。

$("#questions .question").each(function() {
    $(".answer", this).each(function() { // Pass `this` as the context
    });
}
于 2013-03-06T13:41:48.060 に答える