2013-03-06 25 views
0
$("#questions .question").each(function() {} 

.question爲Li,裏面的.question裏是與類.answers,每個.answer類的L1的另一列表。在循環內定位?

我試過這個目標他們上面的循環中,但沒有運氣:

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

有誰知道如何定位用這個循環中的每個答案裏?

回答

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); 
} 
0

使用$(this)

$("#questions .question").each(function() { 
    console.log($(this)); // each .question li 
} 
0
$("#questions .question").each(function() { 
    $(this).find(".answer").each(function() { 
     //code here 
    }); 
} 
0

你可以通過應用選擇上下文。在這種情況下,通過將this實現你想要的:

$("#questions .question").each(function() { 
    $(".answer", this).each(function() { // Pass `this` as the context 
    }); 
} 
0
$("#questions .question").each(function(index, element) { 
    $(element).find('.answer').each(function(index, element) { 
     /* do stuff */ 
    }); 
}); 

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