HTML DOM Document writeln() 方法

定義和用法

writeln() 方法直接寫入打開的 (HTML) 文檔流。

writeln() 方法與 write() 方法相同,只是在每條語句后添加換行符。

警告

writeln() 方法在已加載的文檔上使用時會刪除所有現有的 HTML。

writeln() 方法不能用于 XHTML 或 XML。

另請參閱:

Document write() 方法

Document open() 方法

Document close() 方法

實例

document.writeln("Hello World!");
document.writeln("Have a nice day!");

親自試一試

語法

document.writeln(exp1, exp2, exp3, ...)

參數

參數 描述
exp1, exp2, exp3,
...

可選。輸出流。

允許多個參數,并將按出現的順序追加到文檔中。

返回值

無。

write() 與 writeln() 的差別

writeln() 在每條語句后添加換行符。write() 不會。

實例

document.write("Hello World!");
document.write("Have a nice day!");
document.write("<br>");
document.writeln("Hello World!");
document.writeln("Have a nice day!");

親自試一試

注意

在 HTML 中使用 writeln() 是沒有意義的。它僅在寫入文本文檔 (type=".txt") 時有用。HTML 中會忽略換行符。

如果您希望在 HTML 中換行,你必須使用段落<br>

例子 1

document.write("Hello World!");
document.write("<br>");
document.write("Have a nice day!");

親自試一試

例子 2

document.write("<p>Hello World!</p>");
document.write("<p>Have a nice day!</p>");

親自試一試

瀏覽器支持

所有瀏覽器都支持 document.write

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
支持 支持 支持 支持 支持 支持