有没有办法检查两个DOM元素是否相等? [英] Is there a way to check if two DOM elements are equal?

查看:1271
本文介绍了有没有办法检查两个DOM元素是否相等?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过位置和元素在Javascript中的位置找到元素是没有问题的。
但是有一些比较它们的方法吗?



我可以想到的唯一方法是比较ids或类名,但不是所有元素都有ids或类名

解决方案

如果要比较两个元素指针作为相同的元素,只需使用比较运算符。这可以很容易证明,因为

  document.body === document.body 

例如,如果我以某种方式引用了我不知道的两个元素:

  if(element1 === element2)... 


It's no problem to find an element by position and the position of an element in Javascript. But is there are general way to compare them?

The only way I could think of is comparing ids or classnames, but not all elements have ids or classnames.

解决方案

If you want to compare two element pointers for being the same element, just use the comparison operator. This can be easily proven because

document.body === document.body

For example, if I somehow had references to two elements I didn't know:

if (element1 === element2) ...

这篇关于有没有办法检查两个DOM元素是否相等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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