How come the equation in the title is false? How do check if two jQuery selectors point to the same DOM object?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- How to fix IE ClearType + jQuery opacity problem i
- void before promise syntax
- jQuery add and remove delay
Use $.is()
http://api.jquery.com/is/
You are comparing two distinct jQuery objects because you call
$()
twice (once for each side of the equation), and as MooGoo explains jQuery creates new wrapper objects for each time you call it. That's why the comparison ends up returning false.You can extract a DOM object from each jQuery object by either using
get()
or array dereferencing, then compare these elements. The following both return true because both identical selectors match the samebody
DOM element:If you want to test against a jQuery selector, use
is()
. Note that, unless your selectors are identical, the selectors you use may not necessarily match the same DOM elements (it's still better to use the above). This also returns true:Because jQuery creates a new wrapper object for each
$
call, and in Javascript all objects are distinct, even if they have the exact same properties/methods.On the other hand,
document.body == document.body
would evaluate totrue
.