document.ready中的Javascript函数 [英] Javascript function inside document.ready
本文介绍了document.ready中的Javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么没有在document.ready中写入的任何javascript函数直接从jsp中的事件调用?
Why doesn't any javascript function written inside document.ready be directly called from an event in jsp?
例如:
$(document).ready(function(){
function abc()
{
//Some stuff here
}
});
来自:
<input id="a" type="button" onclick="abc();">
推荐答案
因为它在全球范围内不可用。您作为参数传递给 $ .ready()
的匿名函数中定义的任何函数仅在该函数中可用。
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.
要实现您想要的目标,您需要以下内容:
To achieve what you want to do you need something like:
$(document).ready(function(){
function abc() {}
$('#a').on('click',abc);
});
有关功能范围的更多信息,请参阅此MDN文章
For more information on function scope see this MDN article
这篇关于document.ready中的Javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文