如何调用函数$(document).ready [英] How to call a function inside $(document).ready

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

问题描述

我试图调试使用 jQuery的网络应用程序。

在firebug中调用函数$ (document).ready ..

In firebug im calling functions inside the $(document).ready..

 function val() { console.log('validated outside doc.ready'); }
 $(document).ready(function()
 {

    console.log('document ready...');

    function validate() { console.log('validated!'); }
 }

firebug控制台我键入 validate(),它表示它不是一个函数

In firebug console I type validate() and it says its not a function

如果我键入 val()它工作正常。

If i type val() it works fine.

如何从控制台调用validate?

How do i call validate from the console ?

推荐答案

你不是调用这样的函数,你只需定义函数。

You are not calling a function like that, you just define the function.

正确的方法是定义 document.ready 之外的函数,并在其中调用:

The correct approach is to define the function outside document.ready and call it inside:

// We define the function
function validate(){
  console.log('validated!');
}

$(document).ready(function(){
  // we call the function
  validate();
});

另一个选项是自我调用的功能that:

Another option is to self invoke the function like that:

$(document).ready(function(){
   // we define and invoke a function
   (function(){
     console.log('validated!');
   })();
});

这篇关于如何调用函数$(document).ready的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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