[英]How to addEventListener only to body?
The code below will call the function on every element inside the body.下面的代码将在主体内的每个元素上调用该函数。 But I only want it for body element.但我只想要它的身体元素。
const bodyElement = document.querySelector("body");
bodyElement.addEventListener("click", function() {
console.log("hello");
});
The first argument to the event handler will be an Event object .事件处理程序的第一个参数将是一个Event 对象。
It has a target
property that will tell you the element clicked on.它有一个target
属性,它会告诉你点击的元素。
Compare it to bodyElement
.将其与bodyElement
进行比较。
Try to use this:尝试使用这个:
e.stopPropagation()
If I understand correctly, you want to use this only for one element, so...如果我理解正确,您只想将它用于一个元素,所以......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.