无法访问全局变量(Javascript)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20835209/
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
Can't access global variables (Javascript)
提问by scorpio98
var hours = document.getElementById("hrs").value; // Global variables declared
var mins = document.getElementById("min").value;
var seconds = document.getElementById("sec").value;
function random()
{
alert(hours);
alert(mins);
alert(seconds);
}
The output results as undefined
in all 3 cases.
输出结果与undefined
所有 3 种情况相同。
回答by thefourtheye
This particular code might be within the body of the HTML file and this code is executed before the particular HTML elements are created. So, the the value undefined
is assigned to those values.
此特定代码可能位于 HTML 文件的正文中,并且此代码在创建特定 HTML 元素之前执行。因此,将值undefined
分配给这些值。
So, you might want to move the value assignment part within the function itself.
因此,您可能希望在函数本身内移动赋值部分。
var hours, mins, seconds;
function random()
{
hours = document.getElementById("hrs").value;
mins = document.getElementById("min").value;
seconds = document.getElementById("sec").value;
alert(hours);
alert(mins);
alert(seconds);
}
Note 1:Normally, if you don't use a library like jQuery, code like this is put in onload
. This is what MDN has to say about, when onload is triggered
注 1:通常情况下,如果你不使用像 jQuery 这样的库,这样的代码会放在onload
. 这就是 MDN 必须说的,when onload is triggered
The load event fires at the end of the document loading process. At this point, all of the objects in the document are in the DOM, and all the images and sub-frames have finished loading.
load 事件在文档加载过程结束时触发。至此,文档中的所有对象都在DOM中,所有的图片和子框架都已经加载完毕。
So, if you are getting values from the HTML elements, you might want to make sure that we fetch values from them only after the HTML document is completed loaded.
因此,如果您从 HTML 元素中获取值,您可能希望确保我们仅在 HTML 文档加载完成后才从它们中获取值。
Note 2:Apart from this, you might want to check if you have really set the id
of the HTML elements with hrs
, min
and sec
properly. name
and id
attributes are actually used for different purposes.
注2:除了这个,你可能要检查,如果你真的设置id
与HTML元素的hrs
,min
和sec
正常。name
和id
属性实际上用于不同的目的。
回答by Guillaume Renoult
I had a similar issue recently on a server side script.
我最近在服务器端脚本上遇到了类似的问题。
This was because I was redeclaring the variable inside the function using var. Getting rid of var fixed the issue.
这是因为我使用 var 重新声明了函数内部的变量。摆脱 var 解决了这个问题。
回答by Imran Ali Khan
Try this link, it might be helpful:
试试这个链接,它可能会有所帮助:
Do you know what value will be alerted if the following is executed as a JavaScript program?
你知道下面作为JavaScript程序执行会提示什么值吗?
var foo = 1;
function bar() {
if (!foo) {
var foo = 10;
}
alert(foo);
}
bar();
If it surprises you that the answer is “10”, then this one will probably really throw you for a loop:
如果答案是“10”让您感到惊讶,那么这个答案可能真的会让您陷入循环:
var a = 1;
function b() {
a = 10;
return;
function a() {}
}
b();
alert(a);
http://www.adequatelygood.com/JavaScript-Scoping-and-Hoisting.html
http://www.adequatelygood.com/JavaScript-Scoping-and-Hoisting.html
回答by SantoshK
Easy to way to implement your example as below :
实现您的示例的简单方法如下:
var hours = document.getElementById("hrs").value; // Global variables declared
var mins = document.getElementById("min").value;
var seconds = document.getElementById("sec").value;
function random()
{
alert(hours + mins + seconds);
}
<input type="text" id="hrs" value="9" />
<input type="text" id="min" value=":30" />
<input type="text" id="sec" value=":25" />
<input type="button" id="submit" value="GetDetails" onclick="random();">