Jon*_*nah 82 javascript jquery
我会怀疑这首先工作:
if ($('#element') == $('#element')) alert('hello');
但事实并非如此.如何测试元素是否相同?
chr*_*ton 128
从jquery 1.6开始,你现在可以简单地做到:
$element1.is($element2)
epa*_*llo 74
这应该工作:
if ($(this)[0] === $(this)[0]) alert('hello');
这应该是这样的
if (openActivity[0] == $(this)[0]) alert('hello');
Fra*_*ani 14
要不就
if (openActivity[0] == this) alert('hello');
(没有新的jQuery实例;-)
Ema*_*nde 12
正如有人已经说过的那样,包含在两个不同时刻的相同HTML元素会生成两个不同的jQuery实例,因此它们永远不会相等.
相反,包装的HTML元素可以通过这种方式进行比较,因为如果它们是相同的HTML元素,它们占用的内存位置是相同的,因此:
var LIs = $('#myUL LI');
var $match = $('#myUL').find('LI:first');
alert(LIs.eq(0) === $match); // false
alert(LIs.get(0) === $match.get(0)) // TRUE! yeah :)
最好的祝福!
9 年后,没有 jQuery
如果两个元素相同,则两个元素必须具有相同的指针。因此,
document.body === document.body // true
document.querySelector('div') === document.querySelector('div') // true
document.createElement('div') === document.createElement('div') // false