document.write()
HTML DOM write() 方法
定义和用法
write() 方法可向文档写入 HTML 表达式或 JavaScript 代码。
语法
document.write(exp1,exp2,exp3,...)
参数 | 描述 |
---|---|
exp1,exp2,exp3,... | 可选。要写入的输出流。多个参数可以列出,他们将按出现的顺序被追加到文档中 |
浏览器支持
所有主要浏览器都支持 write() 方法
实例 1
向输出流写入一些文本:
<html>
<body>
<script>
document.write("Hello World!");
</script>
</body>
</html>
<body>
<script>
document.write("Hello World!");
</script>
</body>
</html>
实例 2
向输出流写入一些格式文本:
<html>
<body>
<script>
document.write("<h1>Hello World!</h1><p>Have a nice day!</p>");
</script>
</body>
</html>
<body>
<script>
document.write("<h1>Hello World!</h1><p>Have a nice day!</p>");
</script>
</body>
</html>
实例 3
write() 与 writeln() 的区别:
<html>
<body>
<p>Note that write() does NOT add a new line after each statement:</p>
<pre>
<script>
document.write("Hello World!");
document.write("Have a nice day!");
</script>
</pre>
<p>Note that writeln() add a new line after each statement:</p>
<pre>
<script>
document.writeln("Hello World!");
document.writeln("Have a nice day!");
</script>
</pre>
</body>
</html>
<body>
<p>Note that write() does NOT add a new line after each statement:</p>
<pre>
<script>
document.write("Hello World!");
document.write("Have a nice day!");
</script>
</pre>
<p>Note that writeln() add a new line after each statement:</p>
<pre>
<script>
document.writeln("Hello World!");
document.writeln("Have a nice day!");
</script>
</pre>
</body>
</html>