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.
Ето екранна снимка на това как изглежда: https://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
.
alert.appendChild(button);
, където предупреждението е моят външен div, никъде няма съобщения за грешка. В DOM Explorer във Visual Studio 2012 събитието onclick просто го няма. - person Jacob Morrison   schedule 28.12.2012