Uncaught TypeError: download is not a function at HTMLAnchorElement.onclick (index.html:25)


前段時間調試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]. 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM