前段时间调试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].