HTML之onclick等事件调用函数提示xxx is not defined

现象

如图定义一个onclick点击事件

HTML之onclick等事件调用函数提示xxx is not defined

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

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

就会报错:

 Uncaught ReferenceError: updatemsgconf is not defined
    at HTMLAnchorElement.onclick

HTML之onclick等事件调用函数提示xxx is not defined

 

解决办法:

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

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

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

HTML之onclick等事件调用函数提示xxx is not defined

 总结:

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

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