我正在学习HTML并遇到了一些困难。我无法将其他文字显示在页面The Page上。可以使用帮助,谢谢。 使用:Bluefish编辑器 使用Crome运行页面
<!DOCTYPE>
<html lang="en">
<head>
<title>Temperature</title>
<meta charset="utf-8">
<script>
function showTime() {
var tempByHour = new Array();
tempByHour[0] = 59.2;
tempByHour[1] = 60.1;
tempByHour[2] = 63;
tempByHour[3] = 65;
tempByHour[4] = 62;
for (var i = 0; i < 4; i++) {
var theTemp = tempByHour[i];
var id = "temp" + i;
var li = document.getElementById(id);
if (i == 0) {
li.innerHTML = "The temperature at noon is " + theTemp;
} else {
li.innerHTML = "The temperature at " + i + "was" + theTemp;
}
}
window.onload = showTime
</script>
</head>
<body>
<h1>Temperature</h1>
<ul>
<li id="temp0"></li>
<li id="temp1"></li>
<li id="temp2"></li>
<li id="temp3"></li>
<li id="temp4"></li>
</ul>
</body>
</html>
&#13;
答案 0 :(得分:4)
有时您需要检查控制台,是否有任何错误。您似乎忘了将}
放在showTime
函数的末尾。并且还可以像for (var i = 0; i < tempByHour.length; i++) { ... }
一样更改循环,以覆盖tempByHour
数组的整个长度。
<!DOCTYPE>
<html lang="en">
<head>
<title>Temperature</title>
<meta charset="utf-8">
<script>
function showTime() {
var tempByHour = new Array();
tempByHour[0] = 59.2;
tempByHour[1] = 60.1;
tempByHour[2] = 63;
tempByHour[3] = 65;
tempByHour[4] = 62;
for (var i = 0; i < tempByHour.length; i++) {
var theTemp = tempByHour[i];
var id = "temp" + i;
var li = document.getElementById(id);
if (i == 0) {
li.innerHTML = "The temperature at noon is " + theTemp;
} else {
li.innerHTML = "The temperature at " + i + " was " + theTemp;
}
}
}
window.onload = showTime
</script>
</head>
<body>
<h1>Temperature</h1>
<ul>
<li id="temp0"></li>
<li id="temp1"></li>
<li id="temp2"></li>
<li id="temp3"></li>
<li id="temp4"></li>
</ul>
</body>
</html>
&#13;
答案 1 :(得分:1)
您缺少函数showTime的右括号。在window.onload = showTime
之前添加一个右括号(}),你就完成了!
而且,for for循环条件应该是i <= 4,这样你就可以得到所有点的温度。
答案 2 :(得分:0)
你忘了行上的括号
window.onload = showTime
尝试window.onload = showTime();而是
答案 3 :(得分:0)
您的脚本中有一些错误。请参阅此更新:
<script>
function showTime() {
var tempByHour = new Array();
tempByHour[0] = 59.2;
tempByHour[1] = 60.1;
tempByHour[2] = 63;
tempByHour[3] = 65;
tempByHour[4] = 62;
for (var i = 0; i < tempByHour.length; i++) {
var theTemp = tempByHour[i];
var id = "temp" + i;
var li = document.getElementById(id);
if (i == 0) {
li.innerHTML = "The temperature at noon is " + theTemp;
} else {
li.innerHTML = "The temperature at " + i + "was" + theTemp;
}
}
}
window.onload = showTime();
</script>
这应该可以解决您的问题
答案 4 :(得分:0)
}
缺失。ul
标记后添加脚本。如果在初始化ul
之前包含脚本,则会出现错误,因为未捕获的TypeError:无法设置属性&#39; innerHTML&#39;为null for
循环应该与此for (var i = 0; i <= 4; i++)
类似,或者您可以获取数组的长度。 for (var i = 0; i < 4; i++)
不会显示最后一条记录。您的代码如下所示:
<!DOCTYPE>
<html lang="en">
<head>
<title>Temperature</title>
<meta charset="utf-8">
</head>
<body>
<h1>Temperature</h1>
<ul>
<li id="temp0"></li>
<li id="temp1"></li>
<li id="temp2"></li>
<li id="temp3"></li>
<li id="temp4"></li>
</ul>
<script>
function showTime() {
var tempByHour = new Array();
tempByHour[0] = 59.2;
tempByHour[1] = 60.1;
tempByHour[2] = 63;
tempByHour[3] = 65;
tempByHour[4] = 62;
for (var i = 0; i <= 4; i++) {
var theTemp = tempByHour[i];
var id = "temp" + i;
var li = document.getElementById(id);
if (i == 0) {
li.innerHTML = "The temperature at noon is " + theTemp;
} else {
li.innerHTML = "The temperature at " + i + "was" + theTemp;
}
}
}
window.onload = showTime;
</script>
</body>
</html>
答案 5 :(得分:0)
你在for循环结束时错过了一个} ...
这里代码正确:
<script>
function showTime() {
var tempByHour = new Array();
tempByHour[0] = 59.2;
tempByHour[1] = 60.1;
tempByHour[2] = 63;
tempByHour[3] = 65;
tempByHour[4] = 62;
for (var i = 0; i < 4; i++) {
var theTemp = tempByHour[i];
var id = "temp" + i;
var li = document.getElementById(id);
if (i == 0) {
li.innerHTML = "The temperature at noon is " + theTemp;
} else {
li.innerHTML = "The temperature at " + i + "was" + theTemp;
}
}
}
window.onload = showTime