jQuery测试element1是否为element2的后代 [英] jQuery test if element1 is descendant of element2

查看:88
本文介绍了jQuery测试element1是否为element2的后代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

我不需要element1.isChildOf('selector'),这很容易.
我需要element1.isChildOf(element2)

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

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

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

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

推荐答案

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

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

element2.has(element1).length > 0

这篇关于jQuery测试element1是否为element2的后代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆