document.ready中的Javascript函数

Spu*_*rsP 13 javascript jquery jsp

为什么没有在document.ready中编写的任何javascript函数直接从jsp中的事件调用?

例如:

$(document).ready(function(){
     function abc()
     {
          //Some stuff here
     }
});
Run Code Online (Sandbox Code Playgroud)

来自:

<input id="a" type="button" onclick="abc();">
Run Code Online (Sandbox Code Playgroud)

Mch*_*chl 21

因为它在全球范围内不可用.您作为参数传递的匿名函数中定义的任何函数$.ready()仅在该函数中可用.

要实现您想要的目标,您需要以下内容:

$(document).ready(function(){
     function abc() {}

     $('#a').on('click',abc);
});
Run Code Online (Sandbox Code Playgroud)

有关函数范围的更多信息,请参阅此MDN文章