$("#questions .question").each(function() {}
.question
爲Li,裏面的.question
裏是與類.answers
,每個.answer
類的L1的另一列表。在循環內定位?
我試過這個目標他們上面的循環中,但沒有運氣:
$(this).$(".answer").each(function() {}
有誰知道如何定位用這個循環中的每個答案裏?
$("#questions .question").each(function() {}
.question
爲Li,裏面的.question
裏是與類.answers
,每個.answer
類的L1的另一列表。在循環內定位?
我試過這個目標他們上面的循環中,但沒有運氣:
$(this).$(".answer").each(function() {}
有誰知道如何定位用這個循環中的每個答案裏?
$(this).find('.answer')
或$('.answer', this)
他們是等價的。從jQuery的源:
// HANDLE: $(expr, context)
// (which is just equivalent to: $(context).find(expr)
} else {
return this.constructor(context).find(selector);
}
使用$(this)
:
$("#questions .question").each(function() {
console.log($(this)); // each .question li
}
$("#questions .question").each(function() {
$(this).find(".answer").each(function() {
//code here
});
}
你可以通過應用選擇上下文。在這種情況下,通過將this
實現你想要的:
$("#questions .question").each(function() {
$(".answer", this).each(function() { // Pass `this` as the context
});
}
$("#questions .question").each(function(index, element) {
$(element).find('.answer').each(function(index, element) {
/* do stuff */
});
});