这里写目录标题
- 工具
- 代码
- 运行结果
工具
HBuilder X
代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script>
window.onload = function() // 需要在body加载完成之后,才能通过document获得页面内容
{
var oddsum = 0; // 定义奇数和变量
var evensum = 0; // 定义偶数和变量
var diff = 0; //定义两数之差
for(var i = 0; i <= 100; i++)
{
if(i%2 == 0)
{
evensum += i;
}
else
{
oddsum += i;
}
}
console.log("100以内的偶数和:", evensum);
console.log("100以内的奇数和:", oddsum);
diff = evensum - oddsum;
// console.log(document.getElementById("even"));
// console.log(document.getElementById("even").innerHTML);
document.getElementById("even").innerHTML = evensum;
document.getElementById("odd").innerHTML = oddsum;
document.getElementById("even-odd").innerHTML = diff;
}
</script>
</head>
<body>
<p>
<label>100以内的偶数和:</label><b id="even">0</b>
</p>
<p>
<label>100以内的奇数和:</label><b id="odd">0</b>
</p>
<p>
<label>100以内的偶数和-奇数和:</label><b id="even-odd">0</b>
</p>
</body>
</html>