$(this) with child and pseudo selector
I have the selection below, is there any way to make this selection work with $(this)?
(select the se开发者_运维技巧cond div child of .container)
$(".container div:eq(2)").width();
Try
$(".container").each(function(){
$(this).find("div:eq(2)"); // if its not a direct child
});
What you have there already is fine. Why do you want to use $(this)
? You would use $(this)
as an example, in a function:
$(".container div:eq(2)").click(function() {
if( $(this).width > 100 ) {
...
}
});
What's wrong with:
$(this).find("div:eq(2)").width()
or:
$("div:eq(2)", this).width()
?
BTW that returns the width of the third div descendant from this
(which unlike div:nth-child(2) may or may not have two siblings before it, :eq() refers to the position in the matching pseudo-array, not in the DOM)
精彩评论