开发者

jQuery parents then locate a div to hide

Ok, so i've got this .parents() function that goes outside to a located class/id. In my case, (.wrapper).

<div class="wrapper">
<div class="hide">Hide Class</div>
<div class="boxClass></div>
&开发者_开发问答lt;/div>

I've got a list of these div's on a single page, so, if i click the "Hide Class" text, everything would fadeout, since everything in the list, has the same class name. Now, back to my question. I use .parents() to locate (.wrapper) (i know this can be done with (.parent)). But how can i use .parents to go back and then select (fadeOut) a class inside it? EX, boxClass?


In your case, they're siblings, so just use the siblings()(docs) method in the handler.

$(this).siblings('.boxClass').fadeOut();

Or if they're not actually siblings, use the closest()(docs) method then the find()(docs) method.

$(this).closest('.wrapper').find('.boxClass').fadeOut();

Inside a handler this represents the element that invoked the handler. As such, it is a direct reference to the specific .hide element that was clicked.


Something like this?

$('.hide').parents().children('.boxClass').fadeOut();


.children() only travels a single level down the DOM tree. Use find():

$('.hide').parents().find('.boxClass').fadeOut();
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜