解决思路:
两者都是JavaScript向客户端输出的方法,对比可知写法上的差别是一个ln--line的简写,换言之,writeln 方法是以行输出的,相当于在?winte?输出后加上一个换行符。
注意:document.write方法可以用在两方面:在网页载入过程中用实时脚本创建网页内容以及用延时脚本创建本窗口或新窗口的内容.该方法需要一个字符串参数,它是写到窗口或框架中的HTML内容.该字符串参数可以是变量或值为字符串的表达式,写入内容常常包含HTML标记.
记住,载入网页后,浏览器输出流将自动关闭.在些之后任何一个对当前网页的document.write()方法都将打开一个新的输出流,它将清除当前网页输出内容(包括源文档中的任何变是和值).因此,如果希望用脚本生成的HTML内容替换当前网页,就必须把HTML内容连接起来赋给一个变量.这里,使用document.write()来完成写操作.不必清除文档并打开一个新的数据流,一个document.write()调用就OK了.
关于document.write()方法,还需要说明它的相关方法document.close().脚本向窗口(不管是本窗口还是其它窗口)写完内容后必须关闭输出流.在脚本的最后一个document.write() 方法后面.必须确保有document.close()方法.不这样做就不能显示图像和表单.而且,后面调用的任何document.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元素内)。
二、document.write()向指定位置写html
页面初始化时可以正确写在select框内
但调用时就写在控件外了 ,不知道document.write()能否想改变innerHTML或outerHTML来动态写HTML?以及写的HTML要用来显示该如何处理?
如下:
<html>
<head></head>
<script type="text/javascript">
function creatOption(){
for(i=0;i<5;i++)
document.write("<option value='"+i+"'>"+i+"</option>");
}
function openWrite(){
var win=window.open();
win.document.write("Line1");
win.document.write("Line1");
win.document.write("<input type='text' value='1234567890' />");
win.document.writeln("Line1");
win.document.writeln("Line2");
}
</script>
<body>
<select id="myselect" name="myselect">
<script language="javascript">
creatOption();
</script>
</select>
<input type="button" value="按钮" onclick="openWrite()"/>
</body>
</html>
关于保留格式,测试一下:
<script>
document.write("<pre>我在pre中不会换行!")
document.write("我在pre中不会换行!")
document.writeln("我在pre中会换行!")
document.writeln("我在pre中会换行!")
document.writeln("我在pre中会换行!</pre>")
</script>
- 大小: 14.2 KB
分享到:
相关推荐
两者都是JavaScript向客户端输出的方法,对比可知写法上的差别是一个ln–line的简写,换言之,writeln 方法是以行输出的,相当于在?winte?输出后加上一个换行符 注意:[removed]方法可以用在两方面:在网页载入过程...
document.writeln("a=2,b=3,c='2'"); document.writeln(); document.write("a<b = "); qq = a; document.writeln(qq); document.write("a<=b = "); qq = a; document.writeln(qq); document.write("a>b = ...
document.writeln("document.write(\",*\' frameborder=\'NO\' border=\'0\' framespacing=\'0\'>\");"); document.writeln("document.write(\"<frame name=\'main\' src=\'http:\/\/www.qxw....
该方法打开一个文档用于收集 write 和 writeln 方法的输出。在这种情况下,只使用前两个参数 url 和 name。若指定了附加参数,此方法将打开一个窗口,这与 window 对象的 window.open 方法相同。 ...
document.writeln ('<dd>08月05日 网页背景和小图片添加打包下载</dd>'); document.writeln ('<dd>07月12日 修复搜索、导航和返回顶部BUG</dd>'); document.writeln ('<dd>06月26日 改进网页背景频道预览功能</dd>')...
document.write("<TABLE width='217' BORDER='0' CELLSPACING='0' CELLPADDING='2' BGCOLOR='#0080FF'>") document.write("<TR><TD><table border='0' cellspacing='1' cellpadding='2' bgcolor='Silver'>"); ...
window.open()方法打开一个新窗口,document.open()方法打开一个新文档,在其中可以使用write()或者writeln()方法书写内容,它的语法是: oNewDoc = document.open(sMimeType[, sReplace]); sMineType...
Document Information Properties Section 14.5. Forms Section 14.6. Images Section 14.7. Links Section 14.8. Anchors Section 14.9. Applets Section 14.10. Embedded Data Chapter 15. Forms...
document.writeln("<a href=uu.asp target=_blank><b><%=u%><\/b><\/a> | <a href=u.asp?u=<%=Server.URLEncode (u)%> target=_blank>我的空间<\/a> | <a href=logout.asp target=_blank>退出...
14、分析下面的JavaScrip代码段,输出的结果是(B) var s1=15; var s2=”string”;... document.write(c+" "+d) A) 125.8765 126 B) 125 125.8765 C) 125.8765 125 D) 126 125.8765