jQuery最接近的类选择器 [英] jQuery closest class selector

查看:81
本文介绍了jQuery最接近的类选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆