JQuery find() 相反
我想从输入对象内部开始找到下一个表单元素。 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 jQuery 的 close() 函数来执行此操作。
You can use jQuery's closest() function to do this.
find()
的反面是parents()
。closest()
与find()
并不完全相反,尽管根据您想要执行的操作,它可能适合您。find()
函数查找您指定元素的后代中所有出现的选择器。closest()
仅查找选择器的第一次出现,向上查找指定元素的祖先。因此,与
find()
正确相反的是parents()
,它将查找与指定选择器匹配的元素的所有祖先。The opposite of
find()
isparents()
.closest()
isn't quite an opposite tofind()
, 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 beparents()
, which will find all ancestors of your element that match the specified selector.