jQuery最接近的类选择器 [英] jQuery closest class selector
问题描述
<div class="wrap">
<div class="Level2">Click me</div>
<div class="Level3">Information</div>
</div>
<div class="wrap">
<div class="Level2">Click me</div>
<div class="Level3">Information</div>
</div>
<div class="wrap">
<div class="Level2">Click me</div>
<div class="Level3">Information</div>
</div>
<div class="wrap">
<div class="Level2">Click me</div>
<div class="Level3">Information</div>
</div>
jQuery
$('.Level2').click(function(){
$('.Level2').closest('.Level3').fadeToggle();
});
我想选择最接近fadeIn和fadeOut的level3,但不起作用。我的语法错了吗?在线示例: http://jsfiddle.net/meEUZ/
I wanted to select the closest level3 to fadeIn and fadeOut, but doesn't work. Is my syntax wrong? online Sample :http://jsfiddle.net/meEUZ/
推荐答案
尝试 .next()
而不是 .closest()
遍历DOM元素的祖先。
Try .next()
instead of .closest()
that traverses through the ancestors of the DOM element.
此外,您应该使用 $(this)
而不是 $('。Level2')
否则它将选择所有 .Level2
而不是点击的那个。
Also you should use $(this)
rather than $('.Level2')
else it'll select ALL the .Level2
rather than the clicked one.
你也可以选择这样的东西 - $(this).closest('。wrap')。find('。Level3') .fadeToggle();
。
You can also go for something like this - $(this).closest('.wrap').find('.Level3').fadeToggle();
.
干杯!
这篇关于jQuery最接近的类选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!