2
我想知道兩者相同時id
和function
的效果。例如:如果id和函數名稱相同,則不調用Javascript函數
<tr> <td><img id='deleteAuthor' onclick='javascript: deleteAuthor(this)' src='images/close.png' /></td></tr>";
function deleteAuthor(element){
alert(element);
}
output:TypeError: deleteAuthor is not a function
[Break On This Error]
<tr> <td><img id='deleteAuthorbt' onclick='javascript: deleteAuthor(this)' src='images/close.png' /></td></tr>";
function deleteAuthor(element){
alert(element);
}
output:Object HTMLImageElement
請爲什麼表現得如此?
這是因爲元素ID和函數在全局範圍內碰撞導致元素覆蓋函數引用 –
更多閱讀http://stackoverflow.com/questions/3434278/ie -chrome-are-dom-tree-elements-global-variables- –
[TypeError:prc.cng()不是Firefox中的函數,Uncaught TypeError:Object#中沒有方法'cng' Chrome](http://stackoverflow.com/questions/16581252/typeerror-prc-cng-is-not-a-function-in-firefox-uncaught-typeerror-object) –
Quentin