例一:<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script>
var x=document.getElementById("intro").innerHTML;
document.write("P中内容是:"+x);
</script>
</head>
<body>
<p id="intro"> hello </p>
</body>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script>
var x=document.getElementById("intro").innerHTML;
document.write("P中内容是:"+x);
</script>
</head>
<body>
<p id="intro"> hello </p>
</body>
</html>
结果是:
hello
例二:<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<p id="intro"> hello </p>
<script>
var x=document.getElementById("intro").innerHTML;
document.write("P中内容是:"+x);
</script>
</body>
</html>
结果是:
hello
P中内容是: hello
感想:HTML文件中,程序是从上往下运行的,在例一中,js运行时,p标签的内容还未加载。所以显示不出来。
在例二则能。