简体   繁体   English

检测在没有jQuery的情况下单击了哪些div'id'或'class'

[英]Detect what div 'id' or 'class' was clicked on page without jQuery

Is there a way to write an event on the page to listen for any and all clicks on the page and return their id or class name. 有没有一种方法可以在页面上编写事件以监听页面上的所有点击并返回其ID或类名。 I can't seem to find anything that does it without the use of jQuery. 如果不使用jQuery,我似乎找不到能做到这一点的东西。 Example: 例:

document.body.addEventListener('click', function(event)
    console.log(event.target.id)
    console.log(event.target.className);
});

Seems very simple but I am just having a hard time getting it to work. 看起来很简单,但我很难将其投入使用。 I have tried setting the event listener to the window as well. 我也尝试将事件侦听器设置为窗口。

document.body.addEventListener('click', function(event){
    console.log(event.target.id)
    console.log(event.target.className);
});

missing { 缺少{

I figured it out. 我想到了。 I was clicking inside of an iframe and therefor it wasn't returning any values. 我在iframe中单击,因此没有返回任何值。 Works outside of an iframe like intended. 在预期的iframe外部工作。 Thanks all! 谢谢大家!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM