我不明白“closest()”仍然...请澄清

发布于 2024-12-05 15:25:49 字数 554 浏览 1 评论 0原文

我正在与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

愚人国度 2024-12-12 15:25:49

closest 用于爬上 DOM 树,而不是侧视同级元素。简单来说,它将查看父元素及其父元素,依此类推,直到到达它要查找的内容(或 DOM 树的顶部)。

例如:

<div class="grandparent">
    <div class="parent">
        <div id="child"></div>
        <div class="grandparent"></div>
    </div>
</div>

以下 jQuery 将选择外部 div,而不是同级 div

$(".child").closest(".grandparent");

closestfind,它向下查找 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:

<div class="grandparent">
    <div class="parent">
        <div id="child"></div>
        <div class="grandparent"></div>
    </div>
</div>

The following jQuery will select the outer div, rather than the sibling div:

$(".child").closest(".grandparent");

closest is the opposite of find, which looks down the DOM tree. To select siblings, you can use the siblings method.

紙鸢 2024-12-12 15:25:49

最接近的查找 DOM,如果起始点不在您要查找的范围内,则不会找到它。

我修改了您的代码以显示以下内容:

http://jsfiddle.net/uRgEw/2/

也许您正在寻找这个:

$('.button').click(function(){

alert('hi');

$(this).closest('.container').find('.return_window').html('hi');

});

使用 HTML of:

<div class="container">
    <div class="return_window">

    </div>
    <div class="button">button</div>
</div>

这将使您能够以良好的灵活性上下移动。

如下所示:

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:

$('.button').click(function(){

alert('hi');

$(this).closest('.container').find('.return_window').html('hi');

});

with HTML of:

<div class="container">
    <div class="return_window">

    </div>
    <div class="button">button</div>
</div>

this will let you go up and back down with a good degree of flexibility.

as done here:

http://jsfiddle.net/uRgEw/5/

逆光飞翔i 2024-12-12 15:25:49
<div class="return_window">
    <div class="button">button</div>
</div>

在您的示例中,上面的 HTML 是可行的。它找到最近的祖先。

您可以使用类似的内容:

$(this).next('.return_window').html('hi')

使用您提供的 HTML。

<div class="return_window">
    <div class="button">button</div>
</div>

In your example the above would be the HTML that would work. It finds the closest ancestor.

You could use something like:

$(this).next('.return_window').html('hi')

With the HTML you've provided.

淡莣 2024-12-12 15:25:49

最接近的元素使用 parentNode 从当前选定的元素走到根,并找到与给定选择器匹配的第一个元素:

function closest(element, selector) {
    while (element != null) {
        if (matchesSelector(element, selector)) {
            return true;
        }
        element = element.parentNode;
    }
    return null;
}

例如,我们有这样的 HTML:

<div id="d1" class="a">
  <div id="d2" class="b">
    <div id="d3" class="a">
      <span id="target"></span>
    </div>
  </div>
</div>

并且我们需要一些最接近的选择器,输出将是:

  • 最接近(目标,'.a')-> #d3
  • 最接近(目标,'div')-> #d3
  • 最接近(目标,'.b')-> #d2

closest walks from the current selected element to the root, using parentNode, and find the first element which matches the given selector:

function closest(element, selector) {
    while (element != null) {
        if (matchesSelector(element, selector)) {
            return true;
        }
        element = element.parentNode;
    }
    return null;
}

For example, we have HTML like:

<div id="d1" class="a">
  <div id="d2" class="b">
    <div id="d3" class="a">
      <span id="target"></span>
    </div>
  </div>
</div>

and we need some closest selector, the output would be:

  • closest(target, '.a') -> #d3
  • closest(target, 'div') -> #d3
  • closest(target, '.b') -> #d2
用心笑 2024-12-12 15:25:49

最近搜索元素的 PARENTS - 不是元素附近的兄弟元素。

之所以称为最接近,是因为它找到特定类型(选择器)与该元素最接近的父元素。 .parents() 获取所有父级。

我一直用它。

就像:

<form>
  <div>
    <p>
      <input type="button" onClick="alert($(this).closest('form'));">
    </p>
  </div>
</form>

从内心深处,我可以快速找到父表单,忽略所有中间元素。

如果您想要最近的兄弟姐妹,请执行以下操作:

$(this).prevAll('.return_window').last();
$(this).nextAll('.return_window').first();

这将为您提供上面和下面两个最近的兄弟姐妹,您需要找出哪个更接近。

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:

<form>
  <div>
    <p>
      <input type="button" onClick="alert($(this).closest('form'));">
    </p>
  </div>
</form>

From deep inside I can quickly find the parent form, ignoring all the intervening elements.

If you want the nearest sibling do:

$(this).prevAll('.return_window').last();
$(this).nextAll('.return_window').first();

This will give you the two closest siblings above and below, and it's on you to figure out which is closer.

ヅ她的身影、若隐若现 2024-12-12 15:25:49

.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.

忆离笙 2024-12-12 15:25:49

在您的情况下,使用 next() 而不是 Closest() 作为 Closest() 查找以当前元素开头的树:

$('.button').click(function(){

    $(this).next('.return_window').html('hi');

});

In your case use next() not closest() as closest() looks up the tree BEGINNING with the current element:

$('.button').click(function(){

    $(this).next('.return_window').html('hi');

});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文