document.getElementById和document.write

document.getElementById:

从 JavaScript 访问某个 HTML 元素,可以使用 document.getElementById(id) 方法。

使用 "id" 属性来标识 HTML 元素,并 innerHTML 来获取或插入元素内容:

eg:

<h1>我的第一个 Web 页面</h1>
<p id="demo">我的第一个段落。</p>
<script>
document.getElementById("demo").innerHTML="段落已修改。";
</script>

document.write():

使用 document.write() 仅仅向文档输出写内容。

如果在文档已完成加载后执行 document.write,整个 HTML 页面将被覆盖。

eg:

<h1>我的第一个 Web 页面</h1>
<p>我的第一个段落。</p>
<button onclick="myFunction()">点我</button>
<script>
function myFunction()
{
    document.write(Date());
}
</script>

                                 

来源于: http://www.runoob.com/js/js-output.html

猜你喜欢

转载自www.cnblogs.com/925039847z/p/9892662.html