当前位置: 代码迷 >> Web前端 >> write()跟writeln()换行效果真的有
  详细解决方案

write()跟writeln()换行效果真的有

热度:119   发布时间:2012-09-10 22:20:12.0
write()和writeln()换行效果真的有?

Document对象定义了open()、close()、write()和writeln()四个方法对文档进行创建和修改。

?

? ?write()把文档附加到当前打开的文档。

? ?writeln()把文档附加到当前打开的文档,并输出一个换行符。

?

1、利用上述方法可以输出文本,也可输出Html。

注意:输出Html标签时,尤其是<script>标签,需要将闭合标签进行转义,否则浏览器匹配闭合标签时出错。

?

<script language='"javascript" type="text/javascript">
	document.write('<\/script>');
	document.write('<\/body>');
	document.write('<\/html>');
</script>
? ?

? ?想要输出HTML时不出现错误或乱码,参考这里:http://www.azfly.cn/logs/22/72.html

?

2、两种方法都是将文本按照特定格式输出到Document对象所绑定的终端。然而终端如何解析,他们并不关心。

?

在浏览器中处理文本时,回车符"\n"并不作为实际换行符展示给用户。因此writeln()并不能输出换行的普通文本。

注意:要实现效果就要用Html的"</br>"标记了。

?

<script language="javascript" type="text/javascript">
	window.document.writeln("当前位置:"+window.location+"</br>");
	window.document.writeln("包含窗口个数:"+window.length+"</br>");
	window.document.writeln("当前状态栏的信息:"+window.status+"</br>");
	window.document.writeln("当前窗口的名称:"+window.name+"</br>");
	window.document.writeln("当前窗口的X、Y坐标是:"+window.screenX+","+window.screenY);
</script>
?

?

如果一定要在document.write()当中使用“\n”,则必须搭配HTML的<pre>标记才有作用。

?

<script language="javascript" type="text/javascript">
	document.write( "<pre>想要转行这样\n才行!</pre>");
</script>
  相关解决方案