jQuery点击$(document) - 获取点击的元素 [英] jQuery on click $(document) - get clicked element
本文介绍了jQuery点击$(document) - 获取点击的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想弄清楚如何使用$(document).click()方法获取点击元素乳清:
I'm trying to figure out how to get the clicked element whey using $(document).click() method:
$(document).click(function() {
if ($(this) !== obj) {
obj2.hide();
}
});
在上面的例子中, obj 是对象是下拉菜单 - 如果点击我不想它做任何事情,但如果点击在页面的主体或任何其他元素 - 它应该触发hide()方法。
In the example above the obj is the object is the dropdown menu - and if clicked I don't want it to do anything, but if the click was on the body of the page or any other element - it should trigger the hide() method.
推荐答案
您可以使用 event.target 。您还应该比较DOM元素而不是jQuery对象,因为包含相同元素的两个jQuery对象仍将被视为不同:
You can use event.target. You should also compare DOM elements instead of jQuery objects, since two jQuery objects containing the same elements will still be considered as different:
$(document).click(function(event) {
if (event.target !== obj[0]) {
obj2.hide();
}
});
这篇关于jQuery点击$(document) - 获取点击的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文