本文介绍了将事件侦听器添加到动态创建的div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试添加点击事件监听器到我在动态创建的我的div。
I am trying to add click event listener to my divs that I am creating in my JS dynamically.
我每次创建Div时调用的JavaScript代码片段:
My Javascript snippet of function that is called each time to create the Div:
var listDiv = document.createElement("div");
listDiv.className = "list";
listDiv.addEventListener = ('click',gotoOutcomesLO, false);
点击事件调用的函数:
function gotoOutcomesLO(e){
if(typeof(Storage)!=="undefined"){
var ele = e.target;
var text = ele.getAttribute("name");
sessionStorage.test = text;
}
}
我没有看到任何点击事件添加到我的HTML并不确定出了什么问题。任何指针都会有帮助!谢谢!
I don't see any click events added to my HTML and not sure what's wrong. Any pointers would be helpful! Thanks!
推荐答案
更改 -
listDiv.addEventListener =('click',gotoOutcomesLO, false);
to
listDiv.addEventListener ('click',gotoOutcomesLO,false);
这篇关于将事件侦听器添加到动态创建的div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!