如何比较jquery[副本]中的两个元素

时间:2022-12-29 16:14:01

This question already has an answer here:

这个问题已经有了答案:

var a=$('#start > div:last-child');
var b=$('#start > div.live')[0];
alert(a==b)
alert(a==$(b))

It's always false. How can you compare two elements in jQuery?

它总是错误的。如何在jQuery中比较两个元素?

thanks

谢谢

6 个解决方案

#1


123  

You could compare DOM elements. Remember that jQuery selectors return arrays which will never be equal in the sense of reference equality.

您可以比较DOM元素。请记住,jQuery选择器返回的数组在引用相等的意义上永远不会相等。

Assuming:

假设:

<div id="a" class="a"></div>

this:

这样的:

$('div.a')[0] == $('div#a')[0]

returns true.

返回true。

#2


303  

For the record, jQuery has an is() function for this:

为此,jQuery有一个is()函数:

a.is(b)

Note that a is already a jQuery instance.

注意a已经是jQuery实例。

#3


10  

Every time you call the jQuery() function, a new object is created and returned. So even equality checks on the same selectors will fail.

每次调用jQuery()函数时,都会创建并返回一个新对象。所以即使对相同的选择器进行平等检查也会失败。

<div id="a">test</div>

$('#a') == $('#a') // false

The resulting jQuery object contains an array of matching elements, which are basically native DOM objects like HTMLDivElement that always refer to the same object, so you should check those for equality using the array index as Darin suggested.

生成的jQuery对象包含一个匹配元素数组,这些元素基本上是原生的DOM对象,比如HTMLDivElement,它总是引用相同的对象,所以应该使用Darin建议的数组索引检查它们是否相等。

$('#a')[0] == $('#a')[0] // true

#4


4  

a.is(b)

and to check if they are not equal use

并检查它们是否被使用

!a.is(b)

as for

至于

$b = $('#a')
....
$('#a')[0] == $b[0] // not always true

maybe class added to the element or removed from it after the first assignment

可能类添加到元素中,或者在第一次赋值之后从元素中删除

#5


2  

Random AirCoded example of testing "set equality" in jQuery:

jQuery中测试“set equality”的随机编码示例:

$.fn.isEqual = function($otherSet) {
  if (this === $otherSet) return true;
  if (this.length != $otherSet.length) return false;
  var ret = true;
  this.each(function(idx) { 
    if (this !== $otherSet[idx]) {
       ret = false; return false;
    }
  });
  return ret;
};

var a=$('#start > div:last-child');
var b=$('#start > div.live')[0];

console.log($(b).isEqual(a));

#6


-1  

The collection results you get back from a jQuery collection do not support set-based comparison. You can use compare the individual members one by one though, there are no utilities for this that I know of in jQuery.

从jQuery集合返回的集合结果不支持基于集合的比较。不过,您可以逐个比较各个成员,我在jQuery中没有了解到这方面的实用程序。

#1


123  

You could compare DOM elements. Remember that jQuery selectors return arrays which will never be equal in the sense of reference equality.

您可以比较DOM元素。请记住,jQuery选择器返回的数组在引用相等的意义上永远不会相等。

Assuming:

假设:

<div id="a" class="a"></div>

this:

这样的:

$('div.a')[0] == $('div#a')[0]

returns true.

返回true。

#2


303  

For the record, jQuery has an is() function for this:

为此,jQuery有一个is()函数:

a.is(b)

Note that a is already a jQuery instance.

注意a已经是jQuery实例。

#3


10  

Every time you call the jQuery() function, a new object is created and returned. So even equality checks on the same selectors will fail.

每次调用jQuery()函数时,都会创建并返回一个新对象。所以即使对相同的选择器进行平等检查也会失败。

<div id="a">test</div>

$('#a') == $('#a') // false

The resulting jQuery object contains an array of matching elements, which are basically native DOM objects like HTMLDivElement that always refer to the same object, so you should check those for equality using the array index as Darin suggested.

生成的jQuery对象包含一个匹配元素数组,这些元素基本上是原生的DOM对象,比如HTMLDivElement,它总是引用相同的对象,所以应该使用Darin建议的数组索引检查它们是否相等。

$('#a')[0] == $('#a')[0] // true

#4


4  

a.is(b)

and to check if they are not equal use

并检查它们是否被使用

!a.is(b)

as for

至于

$b = $('#a')
....
$('#a')[0] == $b[0] // not always true

maybe class added to the element or removed from it after the first assignment

可能类添加到元素中,或者在第一次赋值之后从元素中删除

#5


2  

Random AirCoded example of testing "set equality" in jQuery:

jQuery中测试“set equality”的随机编码示例:

$.fn.isEqual = function($otherSet) {
  if (this === $otherSet) return true;
  if (this.length != $otherSet.length) return false;
  var ret = true;
  this.each(function(idx) { 
    if (this !== $otherSet[idx]) {
       ret = false; return false;
    }
  });
  return ret;
};

var a=$('#start > div:last-child');
var b=$('#start > div.live')[0];

console.log($(b).isEqual(a));

#6


-1  

The collection results you get back from a jQuery collection do not support set-based comparison. You can use compare the individual members one by one though, there are no utilities for this that I know of in jQuery.

从jQuery集合返回的集合结果不支持基于集合的比较。不过,您可以逐个比较各个成员,我在jQuery中没有了解到这方面的实用程序。