简体   繁体   English

Javascript变量似乎没有进入DOM

[英]Javascript variable doesn't seem to enter the DOM

<head>
<script>
function setParams() {
    var year = "2011";
}

function showYear() {
    alert("The year is " + year);
}
</script>
</head>
<body>
<input type="submit" onclick="setParams();" value="Set" />
<input type="submit" onclick="showYear();" value="Year" />
</body>

So why does year not enter the dom for alert to read? 那么,为什么年份不输入dom来读取警报呢?

Your variable declaration: 您的变量声明:

var year = "2011";

is a local variable that is only visible inside the function you've declared it in. 是一个局部变量,仅在声明了该变量的函数内部可见。

If you want it visible globally so other functions can access it, then you must declared it as a global variable where you declare it at the top level of scope outside of any functions: 如果要使其全局可见,以便其他函数可以访问它,则必须将其声明为全局变量,并在所有函数之外的作用域的顶层声明它:

var year;

function setParams() {
    year = "2011";
}

function showYear() {
    alert("The year is " + year);
}

A variable declared within a function is, by definition, a local variable and is only available within that function. 根据定义,在函数内声明的变量是局部变量,并且仅在该函数内可用。

Then, lastly you may not quite understand what the DOM is. 然后,最后您可能不太了解DOM是什么。 DOM standards for "Document Object Model" and the term is used to represent the objects in a web page. 用于“文档对象模型”的DOM标准和该术语用于表示网页中的对象。 What you are asking about here is javascript global variable syntax which is separate from the DOM. 您在这里要问的是与DOM分开的javascript全局变量语法。

Hopefully this won't confuse you, but for the sake of completeness of answer, the one place that global Javascript variables and the DOM may appear to be connected is that in a web browser, global variables are also available on the "window" object. 希望这不会使您感到困惑,但是为了完整起见,全局Javascript变量和DOM似乎联系在一起的一个地方是,在Web浏览器中,“ window”对象上也可以使用全局变量。 So, in my code example above, you access year as: 因此,在上面的代码示例中,您将year设置为:

function showYear() {
    alert("The year is " + year);
}

or as: 或为:

function showYear() {
    alert("The year is " + window.year);
}

Both give the same result in a web browser if year is a global variable. 如果year是全局变量,则两者在Web浏览器中给出的结果相同。

var year = "2011";

means the scope of year is local to the function it is declared in 表示年份范围对于在其中声明的功能是局部的

declare it outside the functions. 在函数外声明它。 Or remove the var keyword, which also makes it global. 或删除var关键字,这也使其成为全局关键字。

Your year-var will need a global scope so you can can access it in other functions, ie you define it outside of the functions. 您的year-var将需要一个全局范围,因此您可以在其他函数中访问它,即您可以在函数之外定义它。 Sth like: 像:

var year;

function setParams() {
    year = "2011";
}

function showYear() {
    alert("The year is " + year);
}

Year is scoped to the function setParams and won't be accessible outside of that function. Year的范围是setParams函数,在该函数之外无法访问。

You could change the function to 您可以将功能更改为

function setParams() {
 year = "2011";
}

which would give global access to the year variable 这将使全球可以访问年份变量

Not necessarily best practise though 虽然不一定是最佳做法

Take a look at the module pattern by Douglas Crockford for a useful way to manage global variables and functions: 看一下Douglas Crockford的模块模式,它是管理全局变量和函数的有用方法:

http://yuiblog.com/blog/2007/06/12/module-pattern http://yuiblog.com/blog/2007/06/12/module-pattern

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM