2014-11-06 54 views
-1

我在我的頁面上設置了這個簡單的按鈕,我將在工作時爲其添加不同的功能。但我無法提出警報。它之前在工作,不知道現在有什麼問題。有人可以幫忙嗎?我的按鈕用於工作 - 但現在它不

function sortbutton() 
{ 
    var button = document.createElement("BUTTON"); 
    var text = document.createTextNode("Click To Sort"); 
    button.appendChild(text); 
    document.body.appendChild(button); 
    button.onclick = function selection() 
    { 
     alert("test"); 
    } 
} 
+0

你正在執行你的函數,'sortbutton();'? – dashtinejad 2014-11-06 04:20:25

+0

此代碼看起來不錯,因此請檢查它是否被調用 – TGH 2014-11-06 04:20:58

+0

什麼是瀏覽器控制檯報告? – Kye 2014-11-06 04:21:06

回答

1

我測試了它,它工作正常。也許你應該在文件加載後執行sortbutton()。

<!DOCTYPE html><html> 
<body> 
    <script> 
    function sortbutton(){ 
     var button = document.createElement("button"); 
     var text = document.createTextNode("Click To Sort"); 
     button.appendChild(text); 
     document.body.appendChild(button); 
     button.onclick = function selection() { 
     alert("test"); 
     } 
    } 
    sortbutton(); 
    </script> 
</body></html> 
0

這是我可以忽略的最愚蠢的事情之一 - 你是對的 - 警報被禁用。我一直在研究這個太久。謝謝!

相關問題