2011-06-14 83 views
3

我在HTML頁面中的鏈接,它具有id="redirect"幫助錯誤SCRIPT5007:無法設置的屬性值的「onClick」

而這(的script.js)

window.onload = initAll(); 

function initAll(){ 
    document.getElementById("redirect").onclick = clickHandler; 
} 
function clickHandler(){ 
    alert("Sure!"); 
    return true; 
} 

我收到此錯誤信息

SCRIPT5007: Unable to set value of the property 'onclick': object is null or undefined 

我想這(IE9,IE8,Chrome瀏覽器,火狐4.0.1),但仍不能運行,請大家幫忙

回答

4

你必須寫:

window.onload = initAll; 

在執行點的文檔元素並不存在,因爲你不叫上載功能。您可以調用initAll()並將其返回值分配給window.onload,該值未定義或引發錯誤。

相關問題