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>
答案 0 :(得分:3)
您的<div>
id
panel
而不是Panel
。 ID需要匹配。因此,您可以更改JavaScript代码:
document.getElementById( "panel" )
或HTML:
<div id="Panel"> </div>
答案 1 :(得分:0)
替换
<div id="panel"> </div>
与
<div id="Panel"> </div>
它会起作用。小组中的P应该是资本。