jQuery 选择祖先
是否可以使用 jQuery 选择元素的祖先?
标记:
<div id="ancestor-1">
<div>
<a href="#" class="click-me">Click me</a>
</div>
</div>
<div id="ancestor-2">
<div>
<a href="#" class="click-me">Click me</a>
</div>
</div>
脚本:
$(".click-me").click(function(){
// var ancestorId = ???;
alert(ancestorId)
});
Is is possible to use jQuery to select an ancestor of an element?
Markup:
<div id="ancestor-1">
<div>
<a href="#" class="click-me">Click me</a>
</div>
</div>
<div id="ancestor-2">
<div>
<a href="#" class="click-me">Click me</a>
</div>
</div>
Script:
$(".click-me").click(function(){
// var ancestorId = ???;
alert(ancestorId)
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
尝试
parent()
作为直接父元素。或
parents()
对于所有匹配的祖先元素。或者
closest()
表示第一个最接近的匹配元素(祖先或自身)。parents() 和closest() 之间的区别很微妙但很重要。
closest()
如果匹配则返回当前元素;parents()
仅返回祖先。您可能不希望返回当前元素。closest()
也只返回一个元素;parents()
返回所有匹配的元素。Try
parent()
for the immediate parent element.Or
parents()
for all matching ancestor elements.Or
closest()
for the first closest matching element (either an ancestor or self).The difference between
parents()
andclosest()
is subtle but important.closest()
will return the current element if it's a match;parents()
returns only ancestors. You many not want the possibility of returning the current element.closest()
also only returns one element;parents()
returns all matching elements.你的意思是这样的吗?
这将搜索所有祖先,直到找到匹配项。
http://api.jquery.com/closest/
编辑:
杰罗姆,你的问题可以有多种解释。这说明了 jQuery 的强大功能和灵活性。
请考虑以下事项。
首先,回答你的问题,是的,可以使用 jQuery 选择元素的祖先。
我认为我们可以假设您知道 jQuery 通过以下方式选择任何元素(无论是祖先还是后代)的能力:
给定 click-me 示例,如果您希望返回一个元素的所有祖先的集合,请使用:
或
But请注意,这将遍历所有祖先,返回所有或给定选择器时匹配的所有祖先。
如果您想返回直接父级,请使用:
如果您知道需要哪个祖先,请使用:
但请注意,它只会返回第一个匹配项,如果当前元素(this)是匹配项,它将返回那。
我希望这有帮助。
You mean something like this?
This will search through all ancestors until a match is found.
http://api.jquery.com/closest/
EDIT:
Jerome, your question can be interpreted several ways. This speaks to the power and flexibility of jQuery.
Please consider the following.
First, to answer your question, yes, it is possible to use jQuery to select an ancestor of an element.
I think we can assume that you are aware of jQuery's ability to select any element, whether ancestor or descendant via:
Given the click-me example, if you would like to have a set of all of an element's ancestors returned, use:
or
But be aware that this will traverse through ALL ancestors returning all, or all that match when a selector is given.
If you would like to have the immediate parent returned, use:
If you know which ancestor you need, use:
But be aware that it will only return the first match, and if the current element (this) is a match, it will return that.
I hope this helps.
尝试结合使用parents()或closest(),也许与选择器结合使用来确定哪个祖先应该匹配。例如,查找具有 id 的最接近的祖先 div。
Try using parents() or closest() in combination, perhaps, with a selector to determine which ancestor should match. For example, find the closest ancestor div with an id.
您在寻找
parent()
吗? jQuery Doc 用于父选择器。如果您的场景有所不同,请解释您的预期输出是什么。Are you looking for
parent()
? jQuery Doc for parent selector. If it is different for your scenario explain what is your expected output.http://api.jquery.com/parent/ 和 http://api.jquery.com/parents/
将返回带有 id 的 div
http://api.jquery.com/parent/ and http://api.jquery.com/parents/
would return the divs with the ids
这实际上取决于您想要实现的目标。您想搜索所有祖先,无论使用什么类别吗?或者您想搜索所有祖先元素并具有特定类别(在您的情况下为ancestor-x)?
如果你想循环遍历任何祖先,只需使用 .parent() (有一个很好的例子循环遍历所有元素)或 .parents() 您可以使用它,例如:
可能是最好的方法将使用
.parents()
直到到达具有特定 id 或类的元素。这实际上取决于你想做什么。It really depends what you want to achieve. Do you want to search for all ancestors regardless of class used? Or do you want to search for all elements that are ancestors and have certain class (ancestor-x in your case)?
If you want to cycle through any ancestors, simply use .parent() (there's an excellent example how to cycle through all elements) or .parents() which can you use like:
Probably the best approach would be to use
.parents()
until you get to element with certain id or class. It really depends on what you want to do.