2012-12-28 13 views
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,並沒有什麼似乎工作。該按鈕在課程和文本中顯示得很好。的addEventListener編程爲空

編輯:所以基本上我想要做的是編程方式顯示的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屬性。

+0

您是否將元素添加到DOM?什麼瀏覽器(舊版本的IE不支持addEventListener)? – jfriend00

+0

任何錯誤消息? – Shmiddty

+0

是的,正如我所說的類/文本顯示'alert.appendChild(按鈕);'警報是我的外部div,任何地方都沒有錯誤消息。在Visual Studio 2012中的DOM Explorer中,onclick事件不在那裏。 –

回答

0

的問題是,我是創造了我的「警報」模板div,然後設置另一個股利該div的innerHTML。所以它不允許我設置事件監聽器,因爲它不是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節點相等。

-2

我想你需要添加你的按鈕之前設定的事件監聽器。

+0

不,不會導致問題。 –

+0

我不明白如果對象沒有連接,瀏覽器如何構造消息隊列。 – HMarioD

+0

您可以在將元素插入DOM之前將事件綁定到元素。事實上,這是一個最佳實踐。如果您可以在綁定事件時避免DOM遍歷,那麼您應該這樣做 - 效率更高。 –

0

很難說你的解決方案可能是什麼。你已經提供了很多關於想要用於點擊按鈕的詳細信息,但恐怕還有其他問題需要解決。我不知道你是否在按鈕前有一個元素,使它不會接收到鼠標點擊。我看到你在Windows 8的WinJS項目中。你在VS2012中有很棒的開發工具。在您將按鈕添加到DOM並轉到DOM Explorer後查看是否找到該按鈕即可中斷。轉到JavaScript控制檯並查看是否可以訪問該按鈕。看看你是否可以在那裏手動添加事件偵聽器。嘗試在標記中手動添加按鈕,然後查看添加事件是否有效。希望其中的一個可以讓你找到解決方案。祝你好運。