var button = document.createElement("button");
button.type = "button";
button.className = "button";
button.innerText = "OK";
button.addEventListener("click", function () {
console.log("Hello!");
}, false);
当我这样做时,按钮永远不会得到那个事件监听器。我试过attachEvent
, button.onclick
,但似乎没有任何效果。该按钮与类和文本一起显示得很好。
编辑:所以基本上我想要做的是以编程方式显示一个“弹出”数组的div。
这是它的截图:http://i.imgur.com/IqaOq.png,我将其设置为:var x = new JMTK.Custom.MessageDialog()
,然后添加一个弹出窗口,我只需键入x.addMessage({template: {type: JMTK.Custom.MessageDialog.templates.alert, title: "Alert title", message: "This is a message here", button1: {text: "Hello"}}})
这是addMessage()
:
var content = document.createElement("div");
//htmlObject.template is the object that has all the info, 'this' is the scrim element that contains each "white" popup"
content.innerHTML = MessageDialogClass.html.alert(htmlObject.template, this).innerHTML
调用此函数:
alert: function (template, element) {
//Array of functions
var callbacks = MessageDialogClass.callbacks;
var alert = document.createElement("div");
var id = Date.now();
alert.id = id;
var header = document.createElement("h1");
header.innerText = (template.title ? template.title : "ALERT");
var paragraph = document.createElement("p");
paragraph.innerText = (template.message ? template.message : "No message specified")
var button = document.createElement("button");
button.type = "button";
button.className = "button";
button.innerText = (template.button1.text ? template.button1.text : "OK");
button.addEventListener("click", function () {
if (template.button1.callback) {
template.button1.callback();
}
//MessageDialogClass.popElement(id);
//delete callbacks.id;
}, false);
alert.appendChild(header);
alert.appendChild(paragraph);
alert.appendChild(button);
callbacks.id = alert;
return alert;
},
但是同样,当我单击按钮时,什么也没有发生,并且在 DOM Explorer 中没有onclick
属性。