0

对 javascript 很陌生,并且从书中做了一些示例,无法理解为什么加载时不会在页面上产生任何文本字符串。

function init()
{
var sum = 80 + 20;
var sub = sum - 50;
var mul = sum * 5;
var div = sum / 4;
var mod = sum % 2;
var inc = ++sum;
var dec = --sum;

var str = "Sum: " + sum;
str += "<br>Subtraction: " + sub;
str += "<br>Multiplication: " + mul;
str += "<br>Division: " + div;
str += "<br>Modulus: " + mod;
str += "<br>Increment: " + inc;
str += "<br>Decrement: " + dec;

document.getElementById( "Panel" ).innerHTML = str;
}
document.addEventListener("DOMContentLoaded" , init , false);

以及html5代码;

 <!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<script type="text/javascript" src="arithmetic.js"></script>
<title>Doing Arithmetic</title>
</head>

<body>

<div id="panel"> </div>

</body>

</html>
4

2 回答 2

3

<div>有一个idof panel,不是Panel。ID 需要匹配。因此,您可以更改 JavaScript 代码:

document.getElementById( "panel" )

或 HTML:

<div id="Panel"> </div>
于 2013-10-03T09:51:23.987 回答
0

代替

<div id="panel"> </div>

<div id="Panel"> </div>

它会起作用。面板中的 P 应为大写字母。

于 2013-10-03T09:52:47.153 回答