document.ready 中的 Javascript 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18504253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript function inside document.ready
提问by Pranav Kale
Why doesn't any javascript function written inside document.ready be directly called from an event in jsp?
为什么不直接从jsp中的事件调用document.ready中编写的任何javascript函数?
Eg:
例如:
$(document).ready(function(){
function abc()
{
//Some stuff here
}
});
From something like:
从类似的东西:
<input id="a" type="button" onclick="abc();">
回答by Mchl
Because it's not available in the global scope. Any function defined within the anonymous function you pass as an argument to $.ready()
is only available within that function.
因为它在全局范围内不可用。在您作为参数传递的匿名函数中定义的任何函数$.ready()
仅在该函数中可用。
To achieve what you want to do you need something like:
要实现您想要做的事情,您需要以下内容:
$(document).ready(function(){
function abc() {}
$('#a').on('click',abc);
});
For more information on function scope see this MDN article
有关函数范围的更多信息,请参阅此 MDN 文章