JavaScript在文档中按行输出文本|Document writeln() 方法

Document 对象参考手册


定义与用法

writeln() 方法与 write() 方法作用相同,外加可在每个表达式后写一个换行符。


语法格式

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


示例代码

向输出流写入一些文本:

<script>
document.writeln("Hello World!"); 
</script>

效果展示:


浏览器支持

方法Google ChromeInternet ExplorerFirefoxSafariOpera
writeln()YesYesYesYesYes

所有主要浏览器都支持 writeln() 方法


参数说明

参数描述
exp1,exp2,exp3,…可选.要写入的输出流。它们将按顺序被追加到文档中。

更多示例

write() 与 writeln() 的区别

write()方法为持续输出没有换行:

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

效果展示:


writeln()方法在每个语句后面新增一行:

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

效果展示:

亲自试试


Document 对象参考手册


相关