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!');
});
});
我不知道为什么,但我没有收到提醒信息发现它!
解决方案
您可以使用 jQuery.is
功能:
检查当前匹配的一组元素针对选择器,元素,
或jQuery对象,如果至少有一个元素
匹配给定的参数,则返回true
if(selected_object.is(current_object)){
...
}
另一个解决方案是使用 jQuery.get
函数来获取原始元素,并使用 ==
或 ===
operator:
if(selected_object.get(0)== current_object.get(0)){
...
}
< a href =http:// jsfiddle.net/salman/y8K22/rel =noreferrer> jsFiddle演示
Clicking on an element:
$('.my_list').click(function(){
var selected_object = $(this);
$('.my_list').each(function(){
var current_object = $(this);
if( selected_object == current_object ) alert('FOUND IT !');
});
});
I don't know why, but I don't get the alert message "FOUND IT !".
解决方案
You can use the jQuery.is
function:
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)) {
...
}
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屋!
查看全文