简体   繁体   English

addEventListener function 在 javascript 中不起作用

[英]addEventListener function not working in javascript

I am trying to check if a function that is meant to be triggered by a click with a console.log but the console message never comes up我正在尝试检查 function 是否应该通过单击 console.log 来触发,但控制台消息从未出现

<script src="e-com.js" async></script>

this is how i linked the js file in the head这就是我在头部链接 js 文件的方式

<a href="" id="remove">Remove</a>

this is the link I want the event on这是我想要活动的链接

let removeItem=document.getElementById("remove")
for (i=0; i<removeItem.length; i++){
let  remove = removeItem.addEventListener("click", function(){

  console.log("Clicked");
  })
}

This is the js function这是js function

.getElementById() doesn't return an array since you're not allowed to have more than one element with a single id. .getElementById() 不会返回一个数组,因为您不允许拥有多个具有单个 ID 的元素。

Therefore, the for loop failed.因此,for 循环失败了。

Try尝试

let removeItem=document.getElementById("remove")

removeItem.addEventListener("click", function(){

  console.log("Clicked");
})

The issue seems to be that you are trying to loop over the result of getElementById() , which doesn't return an iterable.问题似乎是您试图遍历getElementById()的结果,它不返回可迭代对象。


To fix it, you just need to remove the loop.要修复它,您只需要删除循环。 The code below should work correctly ^ .下面的代码应该可以正常工作^

 const removeItem = document.getElementById("remove"); removeItem.addEventListener("click", () => { console.log("Clicked;"); });
 <a href="" id="remove">Remove</a>

According to MDN Web Docs :根据MDN Web 文档

TheDocument method getElementById() returns an Element object representing the element whose id property matches the specified string. Document方法getElementById()返回一个Element object,表示其id属性与指定字符串匹配的元素。

As it states, getElementById() returns an Element , which is just an object, in short.正如它所述, getElementById()返回一个Element ,简而言之,它只是一个 object。 This is why you cannot iterate over it.这就是为什么你不能迭代它。


If you wanted to listen to multiple objects with a for loop, you need to change a few things.如果你想用for循环监听多个对象,你需要做一些改变。

First, you can't use an id , as the id attribute is unique, and can only be used once in a HTML document.首先,您不能使用id ,因为id属性是唯一的,并且只能在 HTML 文档中使用一次。 Therefore, to get multiple elements, you need to use the class attribute.因此,要获取多个元素,需要使用class属性。 See an example of the class attribute in use below.请参阅下面使用的class属性的示例。

<div class="division">Division!</div>

The class attribute can be used by any HTML element. class属性可由任何 HTML 元素使用。

So, to get all of the classes and iterate over them, you need to use either the getElementsByClassName() method, or the querySelectorAll() method.因此,要获取所有类并对其进行迭代,您需要使用getElementsByClassName()方法或querySelectorAll()方法。

The only difference between the two is that getElementsByClassName() returns a live HTMLCollection , while querySelectorAll() returns a static HTMLCollection .两者之间的唯一区别是getElementsByClassName()返回一个实时HTMLCollection ,而querySelectorAll()返回一个static HTMLCollection

If you were to use querySelectorAll() , your code would look like this ^ .如果您要使用querySelectorAll() ,您的代码将如下所示^

 const removeItem = document.querySelectorAll("remove"); Array.from(removeItem).forEach((ele) => { ele.addEventListener("click", () => { console.log("Clicked;"); }); });
 <a href="" class="remove">Remove</a> <a href="" class="remove">Remove</a> <a href="" class="remove">Remove</a>


Both of these solutions should work correctly, but they depend on what you need to accomplish.这两种解决方案都应该可以正常工作,但它们取决于您需要完成的工作。


^ The code has been modified slightly. ^代码略有修改。

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

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