开发者

Jquery select child of parent of anchor

开发者 https://www.devze.com 2023-01-10 05:24 出处:网络
I want to select and toggle() the child of the parent of the parent of an anchor. This is an example of what I am trying to do. It doesn\'t work :)

I want to select and toggle() the child of the parent of the parent of an anchor.

This is an example of what I am trying to do. It doesn't work :)

<div>

    <div>
        <a href="#" onclick="$(this + ':parent:parent .inner').toggle();">hide<开发者_开发百科/a>
    </div>

    <div class="inner">
        To be toggled.
    </div>

</div>


You could use .parent() then .siblings(), like this:

$(this).parent().siblings('.inner').toggle();

But, if you're able to make it unobtrusive that'd be a bit more maintainable, for example give your link a class, like this:

 <a href="#" class="toggler">hide</a>

Then you can bind that class (all instances of it), like this:

$(function() {
  $(".toggler").click(function() {
    $(this).parent().siblings('.inner').toggle();
  });
});


$(function(){
    $('a').click(function(){
        $(this).parent().parent().find('.inner').toggle();

    }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消