Javascript $(window).click(function(e) - 当前点击元素的标识符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8875322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:52:53  来源:igfitidea点击:

$(window).click(function(e) - identifier of currently clicked element

javascriptjquery

提问by Mark Fondy

$(window).click(function(e) {
    alert(e.???);
});

How can I check how id, class or other identifiers of the current click?

如何检查当前点击的 id、class 或其他标识符?

回答by

The event object gives you a targetproperty that refers to the actual element clicked, and a currentTargetproperty that refers to the element to which the handler was bound.

事件对象为您提供了一个target属性,该属性指的是单击的实际元素,以及一个currentTarget属性,指的是处理程序绑定到的元素。

Those elements are represented as DOM nodes, which are just objects with their own properties, which enable to you to access certain aspects of the state of the element.

这些元素表示为 DOM 节点,它们只是具有自己属性的对象,使您能够访问元素状态的某些方面。

$(window).click(function(e) {
    alert(e.target.id); // gives the element's ID 
    alert(e.target.className); // gives the elements class(es)
});

回答by bensiu

http://api.jquery.com/category/events/

http://api.jquery.com/category/events/

and look at event.* - 17

并查看事件。* - 17

or console.dir(e);instead alert(e);to see what you have ...

或者console.dir(e);相反alert(e);,看看你有什么......

$(window).click(function(e) {
    console.dir(e);
});