天天看点

JavaScript中Write和Writeln的区别

JavaScript中Write和Writeln的区别

write和writeln的区别

write不可以换行,writeln可以换行。

如何查看writeln的换行效果

在网页中是看不到writeln的换行效果的,它是被浏览器表现为一个空格显示出来了。

JavaScript中Write和Writeln的区别

在html文件和jsp的源文件中都看不到效果,读者可以在标签中加入预格式标签查看效果,

<script> 

   document.write("<pre>write"); 

   document.writeln("writln"); 

   document.write("write</pre>"); 

 </script> 

除了上面这种读者也可以用open方法重新打开一个窗口来查看

<script>  

with(window.open()){  

document.write("write")  

document.writeln("writeln")  

document.writeln("write")  

</script> 

然后在弹出的窗口中查看网页源文件,就可看到效果。笔者经过测试,在chrome 56.0.2924.3中的弹出窗口中没有查看源文件这一栏,这时候可以“检查”然后在element一栏可看到效果,ie11和firefox50.0中都有查看源文件一栏。

注意:

note: document.writeln (like document.write) does not work in xhtml documents

write和writeln在xhtml文件不起作用,html就是语法相对宽松的xhtml,这也就解释为什么在html没有出现换行。点我查看。

作者:hdwk

来源:51cto