将DOM节点与DOM元素进行比较 [英] Comparing a DOM Node wih a DOM Element

查看:102
本文介绍了将DOM节点与DOM元素进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在解决这个问题时遇到了一些麻烦。我知道DOM中的所有内容都是一个节点,而DOM元素也是一个节点。但是,我的问题是这样的:我有一个DOM节点和一个DOM元素,我需要比较这些,以查看它们是否指向相同的元素。这是甚么可能吗?我认为我的问题有根本的错误,但无法弄清楚它是什么。感谢您的帮助。

解决方案

只需执行 === 比较。因为它们都是对象,所以比较将是基于身份的。

  if(my_node === my_element){
//他们是一样的东西
}


I'm having some trouble figuring this problem out. I'm aware that everything in a DOM is a node and that a DOM element is also a node. However, my problem is this: I have a DOM node and a DOM element and I need to compare these to see if they are referring to the same element. Is that even possible? I think there is something fundamentally wrong with my question but can't figure out what it is. Thanks for your help.

解决方案

Just do an === comparison. Since they're both objects, the comparison will be identity based.

if (my_node === my_element) {
    // they're the same thing
}

这篇关于将DOM节点与DOM元素进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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