欢迎光临
我们一直在努力

js错误:xx is not defined at HTMLAnchorElement.onclick原因及解决方法

在做前端超链接onclick编程时,浏览器报错xx is not defined at HTMLAnchorElement.onclick,经过检查发现我把onclick函数写到了jquery的$().ready()中了,这样HTML页面搜索不到该函数,解决方法有两种。

错误代码:

HTML:

  <a onclick='showDetail()'>详情</a>

JS:

  <script>  $().ready(function() {    //其他jquery代码    function showDetail(){      console.log("detail");    }  });  </script>  

方法1:将onclinck函数卸载$().ready()之外。

JS:

  <script>  $().ready(function() {    //其他jquery代码  });  function showDetail(){    console.log("detail");  }  </script>

方法2:采用XXX=function (){}形式

JS:

  <script>  $().ready(function() {    //其他jquery代码    showDetail = function(){      console.log("detail");    }  });  showDetail();  </script>
赞(0)
版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
文章名称:《js错误:xx is not defined at HTMLAnchorElement.onclick原因及解决方法》
文章链接:https://www.ruiwuer.com/399.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。

登录

找回密码

注册