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

原文地址:http://blog.csdn.net/ywl570717586/article/details/53130863

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

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

html:

[html] view plain copy
  1. <input type="button" value="立即登录" onclick="dosave();"/>  

js:

[javascript] view plain copy
  1. dosave = function (){  
  2.         alert("成功啦!");  
  3.     }  

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

[javascript] view plain copy
  1. function dosave(){  
  2.         alert("会报错!!");  
  3.     }  

[javascript] view plain copy
  1. var dosave = function (){  
  2.         alert("会报错!!");  
  3.     }  

为什么会这样,因为:

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

posted @ 2017-08-25 16:17  dirgo  阅读(5333)  评论(2编辑  收藏  举报