jQuery 对象相等
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3176962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery object equality
提问by Casebash
How do I determine if two jQuery objects are equal? I would like to be able to search an array for a particular jQuery object.
如何确定两个 jQuery 对象是否相等?我希望能够搜索特定 jQuery 对象的数组。
$.inArray(jqobj, my_array);//-1
alert($("#deviceTypeRoot") == $("#deviceTypeRoot"));//False
alert($("#deviceTypeRoot") === $("#deviceTypeRoot"));//False
回答by nickf
Since jQuery 1.6, you can use .is
. Below is the answer from over a year ago...
从 jQuery 1.6 开始,您可以使用.is
. 以下是一年多前的回答...
var a = $('#foo');
var b = a;
if (a.is(b)) {
// the same object!
}
If you want to see if two variables are actually the same object, eg:
如果您想查看两个变量是否实际上是同一个对象,例如:
var a = $('#foo');
var b = a;
...then you can check their unique IDs. Every time you create a new jQuery object it gets an id.
...然后您可以检查他们的唯一 ID。每次创建一个新的 jQuery 对象时,它都会获得一个 id。
if ($.data(a) == $.data(b)) {
// the same object!
}
Though, the same could be achieved with a simple a === b
, the above might at least show the next developer exactly what you're testing for.
虽然,同样可以通过一个简单a === b
的 .
In any case, that's probably not what you're after. If you wanted to check if two different jQuery objects contain the same set of elements, the you could use this:
无论如何,这可能不是你想要的。如果你想检查两个不同的 jQuery 对象是否包含相同的元素集,你可以使用这个:
$.fn.equals = function(compareTo) {
if (!compareTo || this.length != compareTo.length) {
return false;
}
for (var i = 0; i < this.length; ++i) {
if (this[i] !== compareTo[i]) {
return false;
}
}
return true;
};
var a = $('p');
var b = $('p');
if (a.equals(b)) {
// same set
}
回答by mauris
If you still don't know, you can get back the original object by:
如果您仍然不知道,您可以通过以下方式取回原始对象:
alert($("#deviceTypeRoot")[0] == $("#deviceTypeRoot")[0]); //True
alert($("#deviceTypeRoot")[0] === $("#deviceTypeRoot")[0]);//True
because $("#deviceTypeRoot")
also returns an array of objects which the selector has selected.
因为$("#deviceTypeRoot")
还返回选择器选择的对象数组。
回答by Roberto Pierpaoli
The $.fn.equals(...)
solution is probably the cleanest and most elegant one.
$.fn.equals(...)
解决方案可能是最干净、最优雅的解决方案。
I have tried something quick and dirty like this:
我尝试过像这样快速而肮脏的事情:
JSON.stringify(a) == JSON.stringify(b)
It is probably expensive, but the comfortable thing is that it is implicitly recursive, while the elegant solution is not.
它可能很昂贵,但令人舒服的是它是隐式递归的,而优雅的解决方案则不是。
Just my 2 cents.
只有我的 2 美分。
回答by Elf King
It is, generally speaking, a bad idea to compare $(foo) with $(foo) as that is functionally equivalent to the following comparison:
一般来说,将 $(foo) 与 $(foo) 进行比较是一个坏主意,因为这在功能上等同于以下比较:
<html>
<head>
<script language='javascript'>
function foo(bar) {
return ({ "object": bar });
}
$ = foo;
if ( $("a") == $("a") ) {
alert ("JS engine screw-up");
}
else {
alert ("Expected result");
}
</script>
</head>
</html>
Of course you would never expect "JS engine screw-up". I use "$" just to make it clear what jQuery is doing.
当然,您永远不会期望“JS 引擎搞砸”。我使用“$”只是为了说明 jQuery 在做什么。
Whenever you call $("#foo") you are actually doing a jQuery("#foo") which returns a new object. So comparing them and expecting same object is not correct.
每当您调用 $("#foo") 时,您实际上是在执行一个 jQuery("#foo")返回一个新对象。所以比较它们并期望相同的对象是不正确的。
However what you CAN do may be is something like:
但是,您可以做的可能是:
<html>
<head>
<script language='javascript'>
function foo(bar) {
return ({ "object": bar });
}
$ = foo;
if ( $("a").object == $("a").object ) {
alert ("Yep! Now it works");
}
else {
alert ("This should not happen");
}
</script>
</head>
</html>
So really you should perhaps compare the ID elements of the jQuery objects in your real program so something like
所以你真的应该在你的真实程序中比较 jQuery 对象的 ID 元素,比如
...
$(someIdSelector).attr("id") == $(someOtherIdSelector).attr("id")
is more appropriate.
更合适。
回答by Remo H. Jansen
Use Underscore.js isEqual method http://underscorejs.org/#isEqual
使用 Underscore.js isEqual 方法http://underscorejs.org/#isEqual
回答by Kareem
First order your object based on key using this function
首先使用此函数根据键订购您的对象
function sortObject(o) {
return Object.keys(o).sort().reduce((r, k) => (r[k] = o[k], r), {});
}
Then, compare the stringified version of your object, using this funtion
然后,使用此功能比较对象的字符串化版本
function isEqualObject(a,b){
return JSON.stringify(sortObject(a)) == JSON.stringify(sortObject(b));
}
Here is an example
这是一个例子
Assuming objects keys are ordered differently and are of the same values
假设对象键的顺序不同并且具有相同的值
var obj1 = {"hello":"hi","world":"earth"}
var obj2 = {"world":"earth","hello":"hi"}
isEqualObject(obj1,obj2);//returns true
回答by Vikram
If you want to check contents are equal or not then just use JSON.stringify(obj)
如果你想检查内容是否相等,那么只需使用 JSON.stringify(obj)
Eg - var a ={key:val};
例如 - var a ={key:val};
var b ={key:val};
var b ={key:val};
JSON.stringify(a) == JSON.stringify(b)-----> If contents are same you gets true.
JSON.stringify(a) == JSON.stringify(b)-----> 如果内容相同,则为真。