我不明白“closest()”仍然...请澄清
我正在与 jQuery 的 closest() 选择器作斗争。
我制作了一个我认为应该有效的jsfiddle...但显然不是。为什么不呢?
HTML
<div class="button">button</div>
<div class="return_window"></div>
和极其复杂的 JS
$('.button').click(function(){
$(this).closest('.return_window').html('hi');
});
我对这个虚幻的 .closest() 选择器有什么不明白的地方?那些 div 看起来很接近我。我已经阅读了文档,但我不知道它是什么。Resig 的文档或编写它的人对我来说似乎处于不同的维度。
任何指示都非常感激。
I am struggling with jQuery's closest() selector.
I've made a jsfiddle of what I think should work... but it clearly doesn't. Why not?
the HTML
<div class="button">button</div>
<div class="return_window"></div>
and the incredibly complex JS
$('.button').click(function(){
$(this).closest('.return_window').html('hi');
});
What don't I understand about this illusive .closest() selector?? Those div's look pretty close to me. I've read the documentation but I don't know what it is.. Resig's documentation or whoever writes it seems to be on a different dimension to me.
Any pointers greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
closest
用于爬上 DOM 树,而不是侧视同级元素。简单来说,它将查看父元素及其父元素,依此类推,直到到达它要查找的内容(或 DOM 树的顶部)。例如:
以下 jQuery 将选择外部
div
,而不是同级div
:closest
与find
,它向下查找 DOM 树。要选择同级,您可以使用siblings
方法。closest
is used to climb up the DOM tree, rather than look sideways at sibling elements. In simple terms, it will look at the parent element, and the parent of that, and so on, until it reaches what it's looking for (or the top of the DOM tree).For example:
The following jQuery will select the outer
div
, rather than the siblingdiv
:closest
is the opposite offind
, which looks down the DOM tree. To select siblings, you can use thesiblings
method.最接近的查找 DOM,如果起始点不在您要查找的范围内,则不会找到它。
我修改了您的代码以显示以下内容:
http://jsfiddle.net/uRgEw/2/
也许您正在寻找这个:
使用 HTML of:
这将使您能够以良好的灵活性上下移动。
如下所示:
http://jsfiddle.net/uRgEw/5/
closest looks up the DOM, if the start point is not inside the one you are looking for it wont find it.
I modded your code to show this:
http://jsfiddle.net/uRgEw/2/
maybe you are looking for this:
with HTML of:
this will let you go up and back down with a good degree of flexibility.
as done here:
http://jsfiddle.net/uRgEw/5/
在您的示例中,上面的 HTML 是可行的。它找到最近的祖先。
您可以使用类似的内容:
使用您提供的 HTML。
In your example the above would be the HTML that would work. It finds the closest ancestor.
You could use something like:
With the HTML you've provided.
最接近的元素使用
parentNode
从当前选定的元素走到根,并找到与给定选择器匹配的第一个元素:例如,我们有这样的 HTML:
并且我们需要一些最接近的选择器,输出将是:
closest walks from the current selected element to the root, using
parentNode
, and find the first element which matches the given selector:For example, we have HTML like:
and we need some closest selector, the output would be:
最近搜索元素的 PARENTS - 不是元素附近的兄弟元素。
之所以称为最接近,是因为它找到特定类型(选择器)与该元素最接近的父元素。
.parents()
获取所有父级。我一直用它。
就像:
从内心深处,我可以快速找到父表单,忽略所有中间元素。
如果您想要最近的兄弟姐妹,请执行以下操作:
这将为您提供上面和下面两个最近的兄弟姐妹,您需要找出哪个更接近。
Closest searches PARENTS of the element - not siblings near the elements.
It's called closest because it finds the closest parent of a particular type (selector) to the element. The
.parents()
get all the parents.I use it all the time.
Like:
From deep inside I can quickly find the parent form, ignoring all the intervening elements.
If you want the nearest sibling do:
This will give you the two closest siblings above and below, and it's on you to figure out which is closer.
.closest()
查找与您的选择器匹配的最接近的祖先。closest()
从自身开始,然后是其父级,或其父级的父级,等等。你所拥有的就是兄弟姐妹。如果您想查找共享相同父级或彼此相邻的元素,则应该使用
.siblings()
。.closest()
finds the closest ancestor that matches your selector.closest()
starts with itself, then its parent, or its parents' parent, or so on.what you have there are siblings. you should use
.siblings()
if you want to find elements that share the same parent, or are next to each other.在您的情况下,使用 next() 而不是 Closest() 作为 Closest() 查找以当前元素开头的树:
In your case use next() not closest() as closest() looks up the tree BEGINNING with the current element: