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

时间:2022-09-01 16:00:26

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.variableName or how can I see this?

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

Thank you in advance.

先谢谢你。

7 个解决方案

#1


18  

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()。

#2


2  

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).ready is scoped to this anonymous function. It's private and not accessible to the outside.

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

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

所以你可以把你的console.log放在$(document).ready中,如果你需要检查一些在其范围内定义的私有变量。

#3


2  

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:

这里有一些链接:

#4


1  

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)一样访问它;或警报(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函数之外使用它们。

#5


1  

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()函数中指定的值。

#6


0  

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.log within document ready, which might give you what you're looking for.

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

#7


0  

If you have

如果你有

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

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

您可以在任何地方看到x变量,但是如果您在文档中声明了变量y就可以在文档准备就绪中访问它:

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

#1


18  

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()。

#2


2  

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).ready is scoped to this anonymous function. It's private and not accessible to the outside.

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

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

所以你可以把你的console.log放在$(document).ready中,如果你需要检查一些在其范围内定义的私有变量。

#3


2  

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:

这里有一些链接:

#4


1  

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)一样访问它;或警报(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函数之外使用它们。

#5


1  

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()函数中指定的值。

#6


0  

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.log within document ready, which might give you what you're looking for.

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

#7


0  

If you have

如果你有

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

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

您可以在任何地方看到x变量,但是如果您在文档中声明了变量y就可以在文档准备就绪中访问它:

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