Javascript jQuery 文档中的调用函数准备就绪

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8142159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 04:51:17  来源:igfitidea点击:

Calling function inside jQuery document ready

javascriptjquery

提问by curly_brackets

While debugging, I always use Firebug and try to call functions and show variables. However I can't when the function or variable is defined within $(document).ready.

在调试时,我总是使用 Firebug 并尝试调用函数和显示变量。但是,当函数或变量在$(document).ready.

How can I access these variables? Can I type something like a namespace, like document.ready.variableNameor how can I see this?

如何访问这些变量?我可以输入命名空间之类的东西,document.ready.variableName或者我怎么能看到这个?

Thank you in advance.

先感谢您。

回答by millimoose

Global variables and functions can be created by assigning them as a property of window:

全局变量和函数可以通过将它们分配为以下属性来创建window

$(function(){
    window.foo = function foo() {
        // …
    }
});

foo()should be accessible anywhere after that handler is executed.

foo()执行该处理程序后,应该可以在任何地方访问。

回答by Darin Dimitrov

How can I access these variables?

如何访问这些变量?

Well, you can't. Everything that you define inside an anonymous function such as what you are using in the $(document).readyis scoped to this anonymous function. It's private and not accessible to the outside.

嗯,你不能。您在匿名函数中定义的所有内容(例如您在 中使用的内容)$(document).ready都限于此匿名函数。它是私人的,外部无法访问。

So you could put your console.loginside the $(document).readyif you needed to inspect some private variable that is defined in its scope.

因此console.log$(document).ready如果您需要检查在其范围内定义的某些私有变量,则可以将其放入内部。

回答by topek

That's what debugging is for. In all major browsers (including IE), you can set breakpoints in the javascript code. When this is done the script halts and you can inspect your variables.

这就是调试的目的。在所有主流浏览器(包括 IE)中,您都可以在 javascript 代码中设置断点。完成后,脚本停止,您可以检查变量。

Here some links:

这里有一些链接:

回答by James Jithin

Declare the variable in global scope:

在全局范围内声明变量:

E.g.

例如

<script type="text/javascript">
var globalVar = null;
$(document).ready(
function() {
     globalVar = "This is the value";
}
);

function TestFunc() {
    alert(globalVar);
}
</script>

Here, if you call the TestFunc()anytime after the page load, you will see the value assigned in the ready() function.

在这里,如果您TestFunc()在页面加载后随时调用,您将看到在 ready() 函数中分配的值。

回答by arb

It depends on how you declare the variables inside the .ready()function. If you do var x = "test", then no, they are only accessible inside the scope of the ready function. If you do something like x="test", then that is available in the global scope and you can just access it like alert(x);or alert(window.x);

这取决于您如何在.ready()函数内声明变量。如果你这样做var x = "test",那么不,它们只能在 ready 函数的范围内访问。如果你做类似的事情x="test",那么它在全局范围内可用,你可以像alert(x);alert(window.x);

You probably don't want to define variables inside the ready function though if you are trying to use them outside the ready function.

如果您尝试在 ready 函数之外使用它们,您可能不想在 ready 函数中定义变量。

回答by James Johnson

Not sure I fully understand the issue, but couldn't you just declare the variables outside of document ready?

不确定我是否完全理解这个问题,但你不能只声明文档之外的变量准备好了吗?

var a = "bar";

$(document).ready(function(){
    a = "foo";
});

If you're using firebug, you should be able to call console.logwithin document ready, which might give you what you're looking for.

如果您使用的是 firebug,您应该能够console.log在文档就绪内调用,这可能会为您提供所需的内容。

回答by Mr_Nizzle

If you have

如果你有

var x = "foo"
$(document).ready(function(){
  alert(x); // foo
});

You can see the xvariable anywhere, but if you you declare a variable yinto the document ready It'll be only accessible within the document ready:

你可以在x任何地方看到这个变量,但是如果你y在文档中声明一个变量ready 它将只能在文档中访问 ready:

var x = "foo"
$(document).ready(function(){
  alert(x); // foo
  var y = "bar"
  alert(y); // bar
});
alert(y); // undefined