jQuery 测试 element1 是否是 element2 的后代

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2398827/
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 13:25:33  来源:igfitidea点击:

jQuery test if element1 is descendant of element2

jquery

提问by Jake

Does anyone know a good way to test if one element, stored in a var, is the descendant of another, also stored in a var?

有谁知道一种测试存储在 var 中的元素是否是另一个也存储在 var 中的元素的后代的好方法?

I don't need element1.isChildOf('selector'), that's easy.
I need element1.isChildOf(element2)

我不需要element1.isChildOf('selector'),这很简单。
我需要element1.isChildOf(element2)

element2.find(element1).size() > 0Does not seem to work.

element2.find(element1).size() > 0似乎不起作用。

I don't want to have to write a plugin the uses .eachto test each child if I can avoid it.

.each如果可以避免的话,我不想编写一个插件来测试每个孩子。

回答by Max Shawabkeh

If you're using 1.4, and are looking for a descendantrather than a child as your find()example implies, there's a has()method:

如果您使用的是 1.4,并且正在寻找后代而不是您的find()示例所暗示的孩子,那么有一种has()方法:

element2.has(element1).length > 0

回答by cletus

You can use index()for this. It will return -1 if an element isn't in the set. Assuming element1and element2are DOM elements and notjQuery objects:

您可以index()为此使用。如果元素不在集合中,它将返回 -1。假设element1element2是 DOM 元素而不是jQuery 对象:

if ($(element2).children().index(element1) != -1) {
  // it's a child
}

For completeness, to test if something is a descendantand not just a child, it can also be used for that too:

为了完整起见,要测试某事物是否是后代而不仅仅是孩子,它也可以用于:

if ($(element1).parents().index(element2) != -1) {
  // element1 is a descendant of element2
}

回答by DonVaughn

As of Jquery 1.6 you can determine whether a jquery object is a child of another jquery object this way:

从 Jquery 1.6 开始,您可以通过以下方式确定一个 jquery 对象是否是另一个 jquery 对象的子对象:

element2.is(element1.children());

Also notable, since is()now accepts several different arguments (the first argument can be a function, jQuery selector, or DOM element), you can't think of the it simply as "is" anymore, but rather "is or is in".

同样值得注意的是,由于is()现在接受几个不同的参数(第一个参数可以是函数、jQuery 选择器或 DOM 元素),您不能再将它简单地视为“is”,而是“is or is in”。

回答by Justin Bull

I know this thread is old but this also works if you want to check upwards not downwards:

我知道这个线程很旧,但如果你想向上而不是向下检查,这也有效:

function isElementChildOf(childElement, parentElement) {
    return 0 !== $(childElement).parents(parentElement).length;
}

Here is a working example

这是一个工作示例

回答by Justin Bull

This is trueif element1 is the child of element2, in other words element1 has a parent, that is element2

这是true如果 element1 是 element2 的子级,换句话说 element1 有一个父级,即 element2

$(element1).parents(element2).length 

this way it is not OK, if the parent has more children (let say they are input's):

这种方式,也不行,如果家长有更多的孩子(让说,他们input):

$(element2).children(element1).length