简体   繁体   中英

How to pick up data- * in the parent from the child

<header data-ride="share">
 <div class="child__one">
   <div class="child__two">
     <div class="child__three"></div>
   </div>
 </div>
</header>

Is possible to catch the header tag from the child__three??

For example:

var parent = $('.child__three').parents('[data-ride="share"]');

This won't work at all.

Thanks.

Use jQuery closest()

var parent = $('.child__three').closest('[data-ride="share"]');

The problem with parents() is it can return multiple objects.

See Difference between jQuery parent(), parents() and closest() functions

DEMO

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