我有這樣的功能:如何將點擊事件添加到我在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