我有一些代碼,通過JavaScript動態創建一個新的按鈕,當點擊時調用JavaScript函數。代碼的工作方式和Firefox,Chrome,Opera一樣,但是低得多,它在IE中不起作用(我甚至不打算用IE6)。IE動態添加按鈕沒有調用點擊
該按鈕被創建並顯示,但它不會調用onclick事件。
var newButton = document.createElement('input');
newButton.setAttribute('id','btnChat_');
newButton.setAttribute('type','button');
newButton.setAttribute('onclick','askQuestion()');
newButton.setAttribute('value', 'Respond');
document.getElementById('frmChat').appendChild(newButton);
有誰知道爲什麼這不會在IE瀏覽器,或有一個建議,我怎麼能寫這個代碼,以便它是否行得通呢?
謝謝。
錯誤:您無法在IE中使用setAttribute()設置任何內聯事件處理程序。 – scunliffe 2009-08-21 03:28:13