Loop什么都没做

Loop not doing anything

本文关键字:什么 Loop      更新时间:2023-09-26

我正试图循环遍历一个html对象数组,并为每个对象添加一个点击事件侦听器。原因是CSP。

问题是:它没有做到,没有错误。。没有什么

在我发疯之前请帮帮我。

var linkButtons = document.getElementsByClassName("navItem");
for(var i=0;i<linkButtons.length;i++){
  linkButtons[index].addEventListener("click",function()console.log("e.e");});
}

function()console->function(){console,并确保文档已加载。

更正:

document.addEventListener("DOMContentLoaded", function(){
    var linkButtons = document.getElementsByClassName("navItem");
    for(var i=0;i<linkButtons.length;i++){
        linkButtons[index].addEventListener("click",function(){console.log("e.e");});
    }
});