document.ready 中的 Javascript 函数 [英] Javascript function inside document.ready

查看:42
本文介绍了document.ready 中的 Javascript 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆