jquery: find parent()*n in nested element?

狂风中的少年 提交于 2019-12-11 03:44:16

问题


the question I could never find an answer for.

imagine I have a rather the following structure inside of a div:

<div class="BOX">
    <ul class="menu" role="navigation">
        </li><li><a class="btn" href="#">edit</a></li>
        <li><a class="btn" href="#">manage</a></li>
        <li><a class="btn deleteBtn" href="#">delete</a></li>
    </ul>
</div>

I want to find() div.BOX when I click on the a.deleteBtn. Since I have multiple div.BOX'es on my page I always need to find it with $(this).

so I could easily use trigger.parent().parent().parent() to select the element when inside of the deleteBtn-ClickHandler, but this doesn't look very nice. Is there a cleaner and better way to do so?

thank you.


回答1:


try this:

$(this).closest("div.BOX")



回答2:


or you can use $(this).parents('div.BOX')



来源:https://stackoverflow.com/questions/7022834/jquery-find-parentn-in-nested-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!