简体   繁体   English

addEvenListener在动态按钮上不起作用-javascript

[英]addEvenListener doesn't work on dynamic button - javascript

So I'm currently busy with learning JavaScript. 因此,我目前正忙于学习JavaScript。 I created some dynamic buttons and I tried to add an addEvenListener to it. 我创建了一些动态按钮,并尝试向其添加addEvenListener。 But it doesn't work and I can't figure it out why it doesn't work. 但这不起作用,我无法弄清楚为什么它不起作用。 I'm just testing some stuff and I try to create buttons with values from the localstorage. 我只是在测试一些东西,然后尝试使用localstorage中的值创建按钮。 It is almost working, only the addEvenListener isn't working. 它几乎可以正常工作,只有addEvenListener无法正常工作。 I just want a simple alert with the key from the localstorage. 我只想使用本地存储中的密钥发出简单警报。

for (var i = 0; i <= localStorage.length-1; i++) {
    var categoryButton = document.createElement('input');
    categoryButton.setAttribute('class', 'forumMenu');
    categoryButton.setAttribute('type', 'button');
    categoryButton.setAttribute('name', localStorage.key(i));
    categoryButton.setAttribute('value', localStorage.key(i));
    categoryButton.setAttribute('id', localStorage.key(i));
    categoryButton.addEventListener('click', function(col){
    alert(col);
}(localStorage.key(i)),true);
    forumMenu.appendChild(categoryButton);
}

Does anyone know why it doesn't work? 有谁知道为什么它不起作用?

You're not passing a function to EventTarget. addEventListener() 您没有将函数传递给EventTarget. addEventListener() EventTarget. addEventListener() . EventTarget. addEventListener() You're immediately executing and passing the result of this: 您将立即执行并传递此结果:

function (col) {
  console.log(col);
}(localStorage.key(i))

... which is undefined . ...这是undefined You can ammend that by using the following syntax: 您可以使用以下语法对此进行修正:

categoryButton.addEventListener('click', (function(col) { 
   // return a function to addEventListener
   return function(){
      console.log(col);
   }
})(localStorage.key(i)),true);

Here's a demo: 这是一个演示:

 var input; for (var i = 0; i <= 5; i++) { input = document.createElement('input'); input.type = "button" input.value = i; input.addEventListener('click', function(i) { // return a function to addEventListener return function() { console.log(i); } }(i), true); document.body.appendChild(input); } 

You stored the value in the id attribute, so just use it: 您将值存储在id属性中,因此只需使用它:

 for (var i = 0; i <= localStorage.length-1; i++) {
    var categoryButton = document.createElement('input');
    categoryButton.setAttribute('class', 'forumMenu');
    categoryButton.setAttribute('type', 'button');
    categoryButton.setAttribute('name', localStorage.key(i));
    categoryButton.setAttribute('value', localStorage.key(i));
    categoryButton.setAttribute('id', localStorage.key(i));

    categoryButton.addEventListener('click', function () {alert(this.getAttribute('id'));});
 document.body.appendChild(categoryButton);
 }

In the event handler, this references the <input> element itself, so you can just retrieve the attribute value. 在事件处理程序中, this引用了<input>元素本身,因此您只需检索属性值即可。

Thats because you are assigning that eventlistener to an object that is not still in the DOM (thats because you created this element after the DOM was loaded) 那是因为您正在将该事件监听器分配给不在DOM中的对象(那是因为您在DOM加载后创建了此元素)

Try this: 尝试这个:

categoryButton.onclick = function(col){
    alert(col);
}

You can either declare a function to a var and then reasign this function to the event handler: 您可以将一个函数声明为var,然后将该函数重新分配给事件处理程序:

var myFunction = function(){
    alert("I'm a function");
}

myElement.onclick = myFunction;

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

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