现象

如图定义一个onclick点击事件

如果我的updatemsgconf函数和deletemsgconf函数是用function定义的,并且写在$(functiong{ }) 里面:

function updatemsgconf(id){
}
function deletemsgconf(id){
}

就会报错:

 Uncaught ReferenceError: updatemsgconf is not defined
    at HTMLAnchorElement.onclick

 

解决办法:

用一个全局变量去定义onclick的点击函数就好了。

updatemsgconf = function (id){
}
deletemsgconf = function (id){
}

这样就不会报错了,正常调用

 总结:

1、如果函数写在$(functiong{ })或者$().ready(function () {});内部,那么 onclick="updatemsgconf()"; 时 ,不能写成  function updatemsgconf (){},也不能写成var updatemsgconf = function(){},只能写成updatemsgconf = function(){}。

2、如果函数写在$(functiong{ })外部,就不会有提示未定义的情况发生。其实还是写法上的bug引起的,写在初始化函数内部相当于函数套函数。

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐