document.getElementById和document.write都是输出
但document.write仅仅向文档输出写内容。如果在文档已完成加载后执行 document.write,整个 HTML 页面将被覆盖
而document.getElementById("demo").innerHTML= 改变的只是局部属性。
例如代码:
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>My First Paragraph.</p>
<button οnclick="myFunction()">点击这里</button>
<script>
function myFunction()
{
document.write("糟糕!文档消失了。");
}
</script>
</body>
</html>
点击后变成:
(来源于: http://www.w3school.com.cn/js/js_shuchu.asp)