相同元素的jQuery对象不相等吗? [英] jQuery objects of the same element are not equal?
问题描述
这一定是我所忽略的,但是请查看下面的页面和JavaScript,并告诉我为什么jQuery不会返回true?
This must be something I'm overlooking, but please look at the following page and JavaScript and tell me why, for everything that's holy, jQuery won't return true?
HTML: http://alcatel.scottbuckingham.com/reporting/test.html
<p class="test">hello1</p>
JS: http://alcatel.scottbuckingham.com/reporting/_scripts/collapse .js
;(function($, window, document, undefined) {
var t = $('.test');
var s = $('.test');
console.log(t);
console.log(s);
if (t === s) {
console.log('yes');
}
})(jQuery, window, document);
我花了数小时试图解决这个问题,并将其简化为几乎不起作用的1 === 1
声明.
I've literally spent hours trying to work it out and reduced it to this, almost 1 === 1
statement which won't work.
我们非常感谢您的帮助!
Any help is highly appreciated!
推荐答案
尝试一下-演示-> http ://jsfiddle.net/mohammadAdil/tHjgN/
Try this - Working Demo --> http://jsfiddle.net/mohammadAdil/tHjgN/
if(t.is(s)) {
console.log('yes');
}
或使用===
if (t.get(0) === s.get(0)) { //<--Compare DOM elements instead of jquery object's
console.log('again yes');
}
演示-> http://jsfiddle.net/mohammadAdil/tHjgN/1/
这篇关于相同元素的jQuery对象不相等吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!