比较 jQuery 中的 DOM 对象
我浏览了其他似乎有相同问题的帖子,但事实并非如此。
我有以下内容:
<table>
<tr id="first_row">
<td><span id="first_span">item1</span></td>
<td><span>item2</span></td>
</tr>
<tr id="second_row">
<td><span>item1</span></td>
<td><span>item2</span></td>
</tr>
</table>
jQuery(function() {
var $a = $("#first_row");
var $b = $("#first_span").parents("tr");
var $c = $("#second_row");
});
$a 和 $b
是两个不同的 jQuery 对象,但引用相同的 DOM 对象,其中 $c
是不同的 DOM 对象,但内容与 $a
和 $b
相同。如何比较它们以使 $a
等于 $b
但不等于 $c
?谢谢。
注意:我对 jQuery 和 DOM 相当陌生,所以希望我没有犯任何概念性错误。如果有的话请随时告诉我。
I have looked through the other SO posts that seem to be of the same question but they are not.
I have the following:
<table>
<tr id="first_row">
<td><span id="first_span">item1</span></td>
<td><span>item2</span></td>
</tr>
<tr id="second_row">
<td><span>item1</span></td>
<td><span>item2</span></td>
</tr>
</table>
and
jQuery(function() {
var $a = $("#first_row");
var $b = $("#first_span").parents("tr");
var $c = $("#second_row");
});
$a
and $b
are two different jQuery objects but referring to the same DOM object, which $c
is a different DOM object but of the same content as $a
and $b
. How do I compare them so that $a
is equal to $b
but not to $c
? Thanks.
Note: I am fairly new to jQuery and DOM, so hope that I am not making any conceptual mistakes. Feel free to let me know, if any.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将 DOM 对象本身(位于 jQuery 对象内的数组中)与此进行比较:
jQuery 对象本身始终是单独的对象,因此您必须查看每个 jQuery 对象内实际 DOM 数组的内容。
您可以使用上面显示的数组语法或使用
.get(x)
方法访问 jQuery 对象内的 DOM 对象。.get()
方法有两个数组语法没有的附加功能。如果不向.get()
传递任何参数,它将返回整个数组。如果传递像.get(-1)
这样的负数,它会返回一个从 DOM 元素数组末尾开始计数的 DOM 对象,而不是从头开始计数。编辑1
如果你想比较两个 jQuery 对象中的整个数组(并且可以假设数组中 DOM 对象的顺序相同),你可以通过编写代码来进行比较:
这也可以是通过 jQuery 插件机制使其本身成为 jQuery 方法。
编辑2
我刚刚发现了另一种方法来做到这一点,它是独立于顺序的并且使用 .filter() 方法更简单。
filter 方法可以将 jQuery 对象作为输入。因此,如果我们通过
b
对象过滤a
对象并获得与开始时相同数量的结果,则a
中的每个对象都必须是在b
中。如果b
不包含a
中的每个对象,那么过滤后的对象的长度会更小。在 jQuery 源代码中,如果将 jQuery 对象传递给 filter() 方法,它只会循环遍历一个数组,在另一个数组上调用 $.inArray() ,消除第一个中不在第二个中的对象- 从而为我们做我们的工作。Compare the DOM objects themselves (which are in an array inside the jQuery object) with this:
The jQuery objects themselves are always separate objects so you have to look at the contents of the actual DOM array inside each jQuery object.
You can access the DOM objects inside a jQuery object with either the array syntax shown above or with the
.get(x)
method. The.get()
method has two additional features that the array syntax does not. If you pass no argument to.get()
it returns the whole array. If you pass a negative number like.get(-1)
, it returns a DOM object counting from the end of the DOM element array instead of counting from the beginning.EDIT 1
If you want to compare the whole arrays in two jQuery objects (and it's OK to assume the order of DOM objects in the array would be the same), you could do so by writing code to do that comparison:
This could also be made to be a jQuery method itself via the jQuery plug-in mechanism.
EDIT 2
I just discovered another way to do this that is order independent and simpler using the .filter() method.
The filter method can take a jQuery object as input. So, if we filter the
a
object by theb
object and get the same number of results we started with then every object ina
must be inb
. Ifb
didn't contain every object ina
, then the length of the filtered object would be smaller. In the jQuery source, if you pass a jQuery object to the filter() method, it just loops through the array of one, calling $.inArray() on the other, eliminating the ones from the first that aren't in the second - thus doing our job for us.另一种可能性是
.is
函数: http://jsfiddle.net/pimvdb/LQgCM/1/ 。Another possibility is the
.is
function: http://jsfiddle.net/pimvdb/LQgCM/1/.您可以比较实际的 DOM 元素:
You can compare the actual DOM elements:
您只想比较实际的 DOM 节点,而不是 jQuery 包装器。例如,
You just want to compare the actual DOM nodes, rather than the jQuery wrappers. E.g.,