我看过一个测试模型,
$ b .parents('nodename')。length> 0当您只需要检查一个元素是该名称的任何节点的子节点。
但是一个特定节点呢?你不能测试
$ b.parents($ a).length> 0 $ c $因为jQuery的父代将一个nodename表达式作为参数进行过滤。作为一个很少的背景,我试图测试文档点击事件的目标是否是特定对象的子对象。例如,如果event.target是$ b的子代,则返回true,否则返回false。但是这个函数可能会在以后有其他的影响。
谢谢!
解决方案p>在使用jQuery对象的jQuery祖先我建议
if($(obj1).parents()。index($(obj2))> = 0){ // obj1 obj2的后代}
I'm trying to come up with a reusable JS or jQuery function that would allow me to test if one object is a DOM descendant of another.
I've seen a model of testing for
$b.parents('nodename').length>0Which is fantastic when you only need to check if an element is a child of any node by that name.
But what about a specific node? You can't test
$b.parents($a).length>0Because jQuery parents takes a nodename expression as an argument to filter.
As a little background, I'm trying to test if a document click event's target is a child of a specific object. For instance, if the event.target is a child of $b, return true, otherwise, return false. But this function could have other implications later on.
Thanks!
解决方案In jQuery ancestors using jQuery objects I suggested
if ($(obj1).parents().index($(obj2)) >= 0) { // obj1 is a descendant of obj2 }
更多推荐
在JavaScript或Jquery中测试对象的祖先/后代关系
发布评论