download is not a function

我的html 代码为

<a href="javascript:void(0)"   th:onclick="'javascript:download(\''+${questionDoc.documentUrl}+'\')'" target="_blank"></a>

运行报错:download is not a function

解决方案为:

修改onclick 里面的名称,比如上面的download 改为 download22222() 就好了

探索发现,原因是a 标签的onclick事件会解析为

function(){

    download()

}

而运行该代码的作用域就是 a标签本身

而a标签有一个download 属性

http://www.w3school.com.cn/tags/att_a_download.asp

所以啊。这里运行的download就是 this.download 而这个是string 空字符串。我们现在却要运行为function就报错了

之前google查了一个类似的错误提示。

http://jsfiddle.net/XpmZG/

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]. 

原文地址:https://www.cnblogs.com/yoable/p/6390429.html 

猜你喜欢

转载自blog.csdn.net/hss0123456789/article/details/88112345