JQuery find() 相反

发布于 2024-12-28 02:56:50 字数 398 浏览 2 评论 0原文

我想从输入对象内部开始找到下一个表单元素。 Find() 是查找子对象的绝佳函数。但是在父类中寻找相反的方法呢?

<form id="grabbMe">
    <div>
        <div>
            <div>
            <div><input type="text" value="test"></div>
            </div>
        </div>
    </div>
</form>

<script>
    $('input').findUp('form').attr('id')
</script>

I want to find the next form-element, starting from input-object inside.
Find() is a great function to find child objects. But what about the opposite Way to find in parent?

<form id="grabbMe">
    <div>
        <div>
            <div>
            <div><input type="text" value="test"></div>
            </div>
        </div>
    </div>
</form>

<script>
    $('input').findUp('form').attr('id')
</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

像极了他 2025-01-04 02:56:50

您可以使用 jQuery 的 close() 函数来执行此操作。

$('input').closest('form').attr('id');

You can use jQuery's closest() function to do this.

$('input').closest('form').attr('id');
弃爱 2025-01-04 02:56:50

find() 的反面是 parents()

closest()find() 并不完全相反,尽管根据您想要执行的操作,它可能适合您。

find() 函数查找您指定元素的后代中所有出现的选择器。

closest() 仅查找选择器的第一次出现,向上查找指定元素的祖先。

因此,与 find() 正确相反的是 parents(),它将查找与指定选择器匹配的元素的所有祖先。

The opposite of find() is parents().

closest() isn't quite an opposite to find(), although depending on what you're trying to do, it may work for you.

the find() function finds all occurances of the selector within the descendants of the element you specify.

closest() only finds the first occurrence of the selector, going upward through the ancestors of the specified element.

So the correct opposite to find() would be parents(), which will find all ancestors of your element that match the specified selector.

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