我正在尝试调试使用 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/