1
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属性。

4

3 回答 3

0

很难说你的解决方案可能是什么。您已经提供了有关单击按钮执行的操作的详细信息,但恐怕还有其他事情在起作用。我想知道您是否在按钮前面有一个元素可以防止它接收鼠标点击。我看到您在 Windows 8 的 WinJS 项目中。您在 VS2012 中有非常好的开发工具。将按钮添加到 DOM 后立即中断,然后转到 DOM Explorer 并查看是否找到该按钮。转到 JavaScript 控制台,看看您是否可以访问该按钮。看看你是否可以在那里手动添加一个事件监听器。尝试在标记中手动添加按钮,然后查看添加事件是否有效。希望其中之一能让您找到解决方案。祝你好运。

于 2012-12-28T16:25:39.520 回答
0

问题是我div在我的“警报”模板中创建了一个,然后将另一个 div 的 innerHTML 设置为该 div。所以它不允许我设置事件监听器,因为它不是 DOM 的一部分。

所以而不是做

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

我已经做了

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 = MessageDialogClass.html.alert(htmlObject.template, this).innerHTML

因为alert已经返回了div。所以,是的,它与设置 innerHTML 而不仅仅是设置它等于 DOM 节点有关。

于 2013-04-01T13:30:44.980 回答
-2

我认为您需要在设置事件侦听器之前附加您的按钮。

于 2012-12-28T00:28:24.763 回答