$("#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 */
});
});