前段時間調試html報了這樣的一個錯誤
Uncaught TypeError: download is not a function at HTMLAnchorElement.onclick (index.html:25)
個人html 代碼爲
<a href="javascript:void(0)" class="down_btn downloadButton" onclick="download()"></a>
解決方案爲:
修改onclick 裏面的名稱,好比上面的download 改成 download22222() 就行了
如下是思考過程
script 爲
function download() {
console.log(‘xxxxx')
}
代碼會報錯
index.html:25 Uncaught TypeError: download is not a function
at HTMLAnchorElement.onclick (index.html:25)
探索發現,緣由是a 標籤的onclick事件會解析爲
function(){
download()
}
而運行該代碼的做用域就是 a標籤自己
<a href="javascript:void(0)" class="down_btn downloadButton" onclick=「console.log(this);download()"></a>
運行後
而a標籤有一個download 屬性
因此啊。這裏運行的download就是 this.download 而這個是string 空字符串。咱們如今卻要運行爲function就報錯了
以前google查了一個相似的錯誤提示。
html以下
Total Storage: <input type="text" name="totalstorage">
Total Bandwidth: <input type="text" name="totalbandwidth">
<input type="button" value="totalbandwidthresult" onclick="debugger;totalbandwidth();">
也是相同的錯誤
在debugger裏面輸入this是當前對象。
而輸入totalbandwidth確實
<input type="text" name="totalbandwidth」>
猜測this的上一級做用域就是document.forms[0].