html页面调用js文件里的函数报错onclick is not defined处理方法

今天处理html标签里的onclick功能的时候总是报错:Uncaught ReferenceError: saveCode is not defined
    at HTMLButtonElement.onclick

找了半天都没发现错在哪,后来通过网上资料https://blog.csdn.net/ywl570717586/article/details/53130863查找得知,正确写法如下:

html:

<button  type="button"onclick="saveCode(this);"><span class="bigger-110">获取验证码</button>

js

saveCode=function (obj){...}

错误写法一般有以下两种,很致命:

function saveCode(){
        alert("会报错!!");
}
var saveCode= function (){
        alert("会报错!!");
}

原因是:html页面调用js文件里的函数,写法必须为dosave = function (){}形式,其他方式写,html页面会搜索不到该函数。

猜你喜欢

转载自blog.csdn.net/qq_39704803/article/details/81868091