我想用 jQuery 获得一个 DOM 元素,但我只想在 DOM 中向后看。
所以假设我有以下 HTML 结构:
<div id="content">
<div id="first-module" class="module">
<h3>Some Module</h3>
<p>Lorem ipsum... </p>
<div id="some-other-content">
<div id="second-module" class="module">
<h3>Some other module</h3>
<p>Dolor sit amet...</p>
<button class="trigger">Trigger 1</button>
</div>
</div>
<button class="trigger">Trigger 2</button>
<div id="third-module" class="module">
<h3>Another module</h3>
</div>
<div>
</div>
$('.trigger').click(function() {
// Traverse DOM to find closest .module class
});
因此,当我单击Trigger 1
按钮时,我希望它找到div
带有 ID的按钮second-module
。
当我单击时,Trigger 2
我希望它找到div
带有 IDfirst-module
而不是 second-module
,因为它的嵌套比Trigger 2
按钮更深。
有没有一个jQuery函数呢?