我有这样的功能:如何将点击事件添加到我在JavaScript中构建的按钮中?
var ButtonChoice = function(key, type, img, label){
this.key = key;
this.type = type;
this.img = img;
this.label = label;
this.getButton = function(){
return "<img src='img/" + this.img + "' type='" + this.type + "' id='" +
this.key + "' class='ButtonsChoice' description='" + this.label +
"' />"
};
};
,我需要点击功能添加到该图像。我的问题是如何做到这一点?
首先将它添加到DOM,然后引用'Element',您可以调用'element.addEventListener('click',function(){// do something});' – Yoda
[Event binding在动态创建的元素?](https://stackoverflow.com/questions/203198/event-binding-on-dynamically-created-elements) – Liam