当前位置:编程学习 > html/css >>

document.write()和document.writeln()的区别

答案:

解决思路:
两者都是JavaScript向客户端输出的方法,对比可知写法上的差别是一个ln--line的简写,换言之,writeln 方法是以行输出的,相当于在winte输出后加上一个换行符。


具体步骤:

1.打开一个空白窗口。
window.open()

2.用 write 方法向空白窗口写入代码。

document.write("Line1")
document.write("Line1")

3.用 writeln 方法向空白窗口写入代码。

document.writeln("Line1")
document.writeln("Line2")

4.完整代码示例:

<script>
with(window.open()){
document.write("Line1")
document.write("Line1")
document.writeln("Line1")
document.writeln("Line2")
}
</script>


注意:两种方法仅当在查看源代码时才看得出区别。
特别提示:把上面的代码加入网页中,然后查看弹出窗口的源代码,将会看到:

Line1Line1Line1
Line2

页面效果和源代码如图。

 

write和writeln方法的输出比较

[page_break]

特别说明

总的来说,一般情况下用两种方法输出的效果在页面上是没有区别的
(除非是输出到<pre>或<xmp>元素内)

附带教程:

document.write()和document.writeln都是JavaScript向客户端写入的方法

writeln是以行方式输出的,一般情况下用两种方法输出的效果在页面上是没有区别的,两种方法仅当在查看源代码时才看得出区别,除非是输出到pre或xmp元素内

测试一下:

<script>
with(window.open()){
document.write("立即")
document.write("立即")
document.writeln("代码")
document.writeln("代码")
document.writeln("代码")
}
</script>

运行上面的代码,在新开的窗口中:查看-源文件,就可以看到,writeln是以行方式输出

关于保留格式,测试一下:

<script>
document.write("<pre>立即")
document.write("立即")
document.writeln("代码")
document.writeln("代码")
document.writeln("代码</pre>")
</script>

上一个:JSP中实现分页
下一个:CODEPAGE=936是什么意思?

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,