使用jQuery对象的jQuery祖先 [英] jQuery ancestors using jQuery objects

查看:64
本文介绍了使用jQuery对象的jQuery祖先的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用两个jQuery对象检查祖先.它们没有ID,并且只能作为jQuery对象(如果您调用get(),则为DOM节点)可用. jQuery的is()仅适用于表达式,因此此代码很理想,但不起作用:

I'd like to check ancestry using two jQuery objects. They don't have IDs, and are only going to be available as jQuery objects (or DOM nodes if you called get()). jQuery's is() only works with expressions, so this code would be ideal but will not work:

var someDiv = $('#div');

$('a').click(function() {
    if ($(this).parents().is(someDiv)) {
        alert('boo');
    }
}

只想看看一个元素是否是另一个元素的子元素,我想尽可能避免回到DOM领域.

Just want to see if one element is a child of another and I'd like to avoid stepping back into DOM land if possible.

推荐答案

您可以使用index()方法来检查列表中是否存在某个元素,那么下面的工作可以吗?

You can use the index() method to check if an element exists in a list, so would the following work?

var someDiv = $('#div');

$('a').click(function() {
    if ($(this).parents().index(someDiv) >= 0) {
        alert('boo');
    }
}

来自 #index参考.

这篇关于使用jQuery对象的jQuery祖先的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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