相同元素的jQuery对象不相等吗? [英] jQuery objects of the same element are not equal?

查看:59
本文介绍了相同元素的jQuery对象不相等吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这一定是我所忽略的,但是请查看下面的页面和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 === 1statement 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');
 }

http://api.jquery.com/is/

或使用===

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屋!

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