Javascript addEventListener以编程方式为空

Javascript addEventListener以编程方式为空,javascript,events,dom-events,winjs,Javascript,Events,Dom Events,Winjs,当我这样做时,按钮永远不会得到该事件侦听器。我尝试了attachEvent,按钮。单击一下,似乎什么都不起作用。该按钮与类和文本一起显示良好 编辑:所以基本上我要做的是以编程方式显示一个div的“弹出”数组 下面是它的屏幕截图:,我这样设置:var x=new JMTK.Custom.MessageDialog(),然后要添加弹出窗口,我只需键入x.addMessage({template:{type:JMTK.Custom.MessageDialog.templates.alert,title

当我这样做时,按钮永远不会得到该事件侦听器。我尝试了
attachEvent
按钮。单击一下,似乎什么都不起作用。该按钮与类和文本一起显示良好

编辑:所以基本上我要做的是以编程方式显示一个div的“弹出”数组

下面是它的屏幕截图:,我这样设置:
var x=new JMTK.Custom.MessageDialog()
,然后要添加弹出窗口,我只需键入
x.addMessage({template:{type:JMTK.Custom.MessageDialog.templates.alert,title:“alert title”,message:“这是一条消息”,按钮1:{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

但是,当我再次单击按钮时,什么也没有发生,在DOM资源管理器中没有单击属性。

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

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

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

因此,与其这样做

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;
},            
我就这么做了

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

因为
警报
已返回
div
。因此,是的,它必须设置innerHTML,而不仅仅是将其设置为与DOM节点相等。

是否将元素添加到DOM中?什么浏览器(IE的旧版本不支持addEventListener)?是的,正如我所说,类/文本显示为
alert.appendChild(按钮)其中alert是我的外部div,任何地方都没有错误消息。在Visual Studio 2012中的DOM Explorer中,onclick事件根本不存在。您向我们透露了一些信息,因为一般概念在这里很好地工作:我将编辑我的原始帖子,并提供完整的细节。不,这不会导致问题。我不知道如果未连接对象,浏览器如何构造消息队列。可以在将元素插入DOM之前将事件绑定到元素。事实上,这是一个最佳实践。如果您可以在绑定事件时避免DOM遍历,那么您应该这样做——效率更高。顺便说一句,我实际上使用了element.onclick,而且我始终相信在元素被调用的那一刻处理程序将被附加。
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