<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="UTF-8">
</head>
<body>
<h1>Green Planet</h1>
<p id="greenplanet">All is well</p>
<h1>Red Planet</h1>
<p id="redplanet">Nothing to report</p>
<h1>Blue Planet</h1>
<p id="blueplanet">All systems A-OK</p>
</body>
</html>
在HTML中插入javascript代码,使得页面完全加载后改变id为“greenplanet”的值:
<script>
function init(){
var planet = document.getElementById("greenplanet");
planet.innerHTML = "Red alert:hit by phaser fire!";
}
window.onload=init;
</script>
如果写成window.onload = init();
就是说你希望调用函数init,如果只是使用名而没有小括号,就会把这个函数值赋给onload属性,输入时差别很细腻,但是含义有天壤之别,所以要格外当心。