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();
}
}
精彩评论