首页 > Web开发 > 详细

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

时间:2017-02-12 11:08:34      阅读:3889      评论:0      收藏:0      [点我收藏+]

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

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

原文:http://www.cnblogs.com/yoable/p/6390429.html

(9)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!