电脑爱好者,提供IT资讯信息及各类编程知识文章介绍,欢迎大家来本站学习电脑知识。 最近更新 | 联系我们 RSS订阅本站最新文章
电脑爱好者
站内搜索: 
当前位置:首页>> Javascript>>Javascript之document.write()和document.writeln()的区别:

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

来源:网络 | 2008-2-10 | (有7447人读过)

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


具体步骤:  

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  

特别说明 

总的来说,一般情况下用两种方法输出的效果在页面上是没有区别的(除非是输出到?pre或xmp?元素内)。 
Javascript热门文章排行
网站赞助商
购买此位置

 

关于我们 | 网站地图 | 文档一览 | 友情链接| 联系我们

Copyright © 2003-2024 电脑爱好者 版权所有 备案号:鲁ICP备09059398号