JQuery,找到父级

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4718409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:48:02  来源:igfitidea点击:

JQuery, find parent

jqueryparent

提问by Andy E

<ul><li><div><div><span id="thisid"></span></div></div></li></ul>

$('#thisid').parent('li');

that obviously doesn't work, but how do I grab the li element? I don't want to use:

这显然不起作用,但是我如何获取 li 元素?我不想使用:

$('#this').parent().parent().parent();

I don't want to use it, because it can happen that there is just one div element, instead of two. In this case I would grab the ul element instead of the li element.

我不想使用它,因为可能只有一个 div 元素,而不是两个。在这种情况下,我将获取 ul 元素而不是 li 元素。

回答by Andy E

$('#thisid').parents('li');
//                 ^ plural!

Note that if you only want the first <li>element in the ancestry, you should use closest():

请注意,如果您只想要<li>祖先中的第一个元素,则应使用closest()

$('#thisid').closest('li');

// `closest()` is equivalent to (but performs better than)
$('#thisid').parents('li').eq(0);
$('#thisid').parents('li').first();

回答by davin

$('#thisid').parents('li')

or if you only want the first one:

或者如果你只想要第一个:

$('#thisid').closest('li')

回答by generalhenry

回答by Jacob Relkin

Simple, use parents()

简单,使用 parents()

var parents = $("#thisid").parents('li');

回答by Decent Dabbler

$('#thisid').parents( 'li:eq(0)' ); 

Should do it. This will give you the first (:eq(0)) parent that matches being the tag you're searching for.

应该做。这将为您提供:eq(0)与您正在搜索的标签匹配的第一个 ( ) 父级。

回答by Uzair Xlade

I prefer the 'closest' than 'parents'.

我更喜欢“最亲近的”而不是“父母”。

Parents travel up the DOM tree to the document's root element, adding each ancestor element to a temporary collection; it then filters that collection based on a selector if one is supplied.

父元素沿着 DOM 树向上移动到文档的根元素,将每个祖先元素添加到一个临时集合中;然后,如果提供了选择器,它会根据选择器过滤该集合。

where

在哪里

Closest Travel up the DOM tree until it finds a match for the supplied selector.

Closest 沿 DOM 树向上移动,直到找到与提供的选择器匹配的对象。

Most important what they give in result:

最重要的是他们给出的结果:

Praents: Returned jQuery object contains zero or more elements for each element in the original set, in reverse document order.

Praents:对于原始集合中的每个元素,返回的 jQuery 对象包含零个或多个元素,文档顺序相反。

Closest: Returned jQuery object contains zero or one element for each element in the original set, in document order

最接近:返回的 jQuery 对象包含原始集合中每个元素的零个或一个元素,按文档顺序排列

$('#thisid').closest('li');

Follow this Link

按照这个链接

回答by Richard Marskell - Drackir

You may actually want to use $("#thisid").closest('li'). This traverses up the DOM tree from the selected node and returns the first matching ancestor whereas the .parents()travels from the parent node and returns all ancestor nodes matching that filter. See herefor more information.

您可能实际上想要使用$("#thisid").closest('li'). 这将从所选节点向上遍历 DOM 树并返回第一个匹配的祖先,而.parents()从父节点开始并返回与该过滤器匹配的所有祖先节点。请参阅此处了解更多信息。