简体   繁体   中英

How to access to a parent node from a child node using JQuery?

I have the following markup:

<div class"cA" >
   <div class="c..">
       <div class="cZ" >
       </div>
   </div>
</div>

How can I from the div which has the class cZ get the div which has class cA , knowing that the number of divs between then is not specific.

var $parent = $('.cZ').parents('.cA');

As others have said, parents() will walk the parent chain and accumulate the elements that match the specified selector. However, if more than one parent element matches the selector (in your case, if more than one parent <div> exposes the cA class), then the jQuery object that is returned will contain more than one element.

If that behavior isn't what you want, consider using closest() instead.

Just use the .parents() method. It should return what you're looking for.

Look in to Jquery Tree Traversal API. The one that help you in this case is .parents()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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