美文网首页
【转】html页面调用js文件里的函数报错onclick is

【转】html页面调用js文件里的函数报错onclick is

作者: 熊本猫 | 来源:发表于2018-12-21 11:09 被阅读0次

原文作者:ywltoread

原文:https://blog.csdn.net/ywl570717586/article/details/53130863

今天处理html标签里的onclick功能的时候总是报错:Uncaught ReferenceError: dosave is not defined(…)

找了半天都没发现错在哪,最后发现原来是我写法不对,正确写法如下:

html:

<input type="button" value="立即登录" onclick="dosave();"/>

js:

dosave = function (){
        alert("成功啦!");
    }

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

function dosave(){
        alert("会报错!!");
    }

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

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

相关文章

网友评论

      本文标题:【转】html页面调用js文件里的函数报错onclick is

      本文链接:https://www.haomeiwen.com/subject/njhmkqtx.html