jQuery比较两个DOM对象? [英] jQuery compare two DOM object?
本文介绍了jQuery比较两个DOM对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
点击某个元素:
$('.my_list').click(function(){
var selected_object = $(this);
$('.my_list').each(function(){
var current_object = $(this);
if( selected_object == current_object ) alert('FOUND IT !');
});
});
我不知道为什么,但我没有收到警告消息FOUND IT! 。
I don't know why, but I don't get the alert message "FOUND IT !".
推荐答案
您可以使用 jQuery.is
函数:
You can use the jQuery.is
function:
检查当前匹配的元素集对选择器元素,
或jQuery对象,如果这些元素中至少有一个
匹配给定的参数,则返回true。
Check the current matched set of elements against a selector, element, or jQuery object and return true if at least one of these elements matches the given arguments.
if (selected_object.is(current_object)) {
...
}
另一种解决方案是使用 jQuery。 get
函数来获取原始元素,并使用 ==
或 ===
operator:
An alternate solution is to use jQuery.get
function to get the raw elements and compare them using ==
or ===
operator:
if (selected_object.get(0) == current_object.get(0)) {
...
}
这篇关于jQuery比较两个DOM对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文