javascript addEventListener not working more than once

让人想犯罪 __ 提交于 2019-12-01 22:20:50

addEventListener takes a function reference as its second parameter. Here, you're directly calling the function when you try to add it. This should work for you:

document.addEventListener('DOMContentLoaded', function () {
    alert("document loaded!");
    sliderRed.addEventListener("change", function(){alert(sliderRed.value)} );
    // wrapped in a function
} );

You'll have to apply this pattern to all of your events.

sliderRed.addEventListener("change", alert(sliderRed.value) ); is immediately invoking the alert function and assigning the return value to your event listener, wrap it in a closure and it will work:

sliderRed.addEventListener("change", function(){
  alert(sliderRed.value) 
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!