简体   繁体   English

jQuery-按类定位下一个元素

[英]jQuery - Target next element by class

I'm tryingto target the next div with class="modal" to display it when i click on the <h4> OR the <div> with class "imgdd". 当我单击类别为“ imgdd”的<h4><div> ,我试图使用class="modal"来显示下一个div。

<li class="itemmenumodal">
    <div class="imgdd"></div>
    <h4>test</h4>                       
    <div class="modal">
        <p>hello there</p>
    </div>
</li>    

I tried this, it works when i click on the <h4> but not for the <div> with class="imgdd" : 我试过了,当我点击<h4>而不是class="imgdd"<div>时,它可以工作:

$(".itemmenumodal h4,.itemmenumodal div.imgdd").click(function(){
    jQuery(this).next(".modal").bPopup({            
        speed: 350,
        transition: 'slideDown',
        closeClass:'closemodal',
    });                 
})

How can I do this? 我怎样才能做到这一点?

.next()将仅获取下一个元素,因此必须使用.nextAll()和:first来获取下一个第一个.modal元素

  jQuery(this).nextAll(".modal:first").bPopup({  

This should do the work: 这应该做的工作:

$(".itemmenumodal h4,.itemmenumodal div.imgdd").click(function(){
    $(jQuery(this).siblings(".modal")[0]).bPopup({            
    speed: 350,
    transition: 'slideDown',
    closeClass:'closemodal',
});                 
})

Here is the jsfiddle: http://jsfiddle.net/3PwHK/ 这是jsfiddle: http : //jsfiddle.net/3PwHK/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM