jquery - 如何调用 $(document).ready 中的函数

标签 jquery debugging firebug

我正在尝试调试使用 jQuery 的 Web 应用程序。

在 firebug 中,我调用 $(document).ready.. 中的函数。

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

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

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

在 firebug 控制台中,我输入 validate() 它说它不是一个函数

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

如何从控制台调用验证?

最佳答案

您没有调用这样的函数,您只是定义该函数。

正确的做法是在document.ready外部定义函数并在内部调用它:

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

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

另一种选择是自调用该函数,如下所示:

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

关于jquery - 如何调用 $(document).ready 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17567176/

相关文章:

javascript - 如何在 if-else 检查 div 位置后添加图像?

javascript - 使用 jQuery 将表单数据转换为 JS 对象,包括分组值

javascript - jQuery 单独切换多个区域

jquery - 使用动态 ID 选择 Multiple SelectManyCheckBox 中的所有项目

c - 如何阅读、理解、分析和调试 Linux 内核 panic ?

css - 如何使用 Eclipse (Windows) 保存 Firebug 更改?

visual-studio - Visual Studio : How to stop breakpoint hit from stealing focus?

debugging - GoLand远程调试显示 "could not find <file>"

selenium - Firebug 和 Selenium : Performance

jquery - 火狐错误-NS_ERROR_XPC_NOT_ENOUGH_ARGS : Not enough arguments