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() 的區別:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>IT研修(xuhuhu.com)</title>
</head>
<body>
<p>注意write()方法不會在每個語句後面新增一行:</p>
<script>
document.write("Hello World!");
document.write("Have a nice day!");
</script>
<p>注意writeln()方法在每個語句後面新增一行:</p>
<script>
document.writeln("Hello World!");
document.writeln("Have a nice day!");
</script>
</body>
</html>
<html>
<head>
<meta charset="utf-8">
<title>IT研修(xuhuhu.com)</title>
</head>
<body>
<p>注意write()方法不會在每個語句後面新增一行:</p>
<script>
document.write("Hello World!");
document.write("Have a nice day!");
</script>
<p>注意writeln()方法在每個語句後面新增一行:</p>
<script>
document.writeln("Hello World!");
document.writeln("Have a nice day!");
</script>
</body>
</html>
