2013-03-06 26 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/