瓶啤酒 - 这是什么:var bottlesDiv =文件

时间:2015-02-06 23:14:34

标签: javascript dom getelementbyid

我已经遵循了一个非常基本的啤酒瓶教程(与本网站上已经提到的不同),我不明白这部分是做什么的:

var bottlesDiv = document.

这里的完整代码(它确实按预期工作):



 

var bottles = 99;
var lyrics = "";
while (bottles > 0) {
lyrics = lyrics + bottles + " bottles of beer on the wall <br>";
lyrics = lyrics + bottles + " bottles of beer <br>";
lyrics = lyrics + "Take one down, pass it around, <br>";
bottles = bottles - 1;
if (bottles > 0) {
	lyrics = lyrics + bottles + " bottles of beer on the wall <br><br>";
} else {
	lyrics = lyrics + "No more bottles of beer on the wall. <br>";
}
}
var bottlesDiv = document.
getElementById("bottles");
bottlesDiv.innerHTML = lyrics;
&#13;
<!doctype html>
<html lang="en">
<head>
	<title>99 Bottles of Beer</title>
	<meta charset="utf-8">
	<link rel="stylesheet" href="style.css">
</head>
<body>
	<h1>99 Bottles of Beer </h1>
	<div id="bottles"></div>
</body>
</html>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:6)

这实际上是一个多行JavaScript语句:

// define the bottlesDiv variable and assign to it
var bottlesDiv = document // start at window.document which is the root of the page
  .getElementById('bottles'); // find the element in document with id = bottles

当写成:

时,它确实更有意义
var bottlesDiv =
  document.getElementById('bottles');

答案 1 :(得分:0)

var bottlesDiv = document.getElementById("bottles");

仅仅是格式错误的情况。 bottlesDiv是持有瓶子id的元素的变量。

getElementById()是一个Javascript选择器函数。

答案 2 :(得分:0)

它不是

var bottlesDiv = document。

它的

var bottlesDiv = document.getElementById(“bottles”);

它读到半结肠。