可能很多朋友都遇到过这样的情况,那就是使用document.write()函数向网页中写内容的时候,会把文档中的原来的内容给清空,这一点对于初学者来说算是一个困扰,下面就介绍一下为什么会出现这种情况,当然也就知道如何避免此种情况的发生了。
先看一段代码实例:
<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <title>Document</title> <script type="text/javascript"> window.onload=function(){ document.write("重温 JavaScript"); } </script> </head> <body> <div>Hello JavaScript</div> </body> </html>
从以上代码的可以看出document.write()函数将原来的文档内容清空了,下面介绍一下出现此种情况的原因:
window.onload事件是在文档内容完全加载完毕再去执行事件处理函数,当然文档流已经关闭了,这个时候执行doucment.writ()函数会自动调用document.open()函数创建一个新的文档流,并写入新的内容,再通过浏览器展现,这样就会覆盖原来的内容。不过很多朋友还有会这样的疑问,为什么类似下面的情况,原来网页中的内容不会被覆盖,代码如下:
<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <title>Document</title> <script type="text/javascript"> document.write("重温 JavaScript"); </script> </head> <body> <div>Hello JavaScript</div> </body> </html>
在以上代码中,原来的文档内容并没有被清空,这是因为当前文档流是由浏览器所创建,并且document.wirte()函数身处其中,也就是执行此函数的时候文档流并没有被关闭,这个时候不会调用document.open()函数创建新文档流,所以也就不会被覆盖了。可能还有朋友会问为什么下面的方式还是不行,代码如下:
<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <title>Document</title> <script type="text/javascript"> document.close(); document.write("重温 JavaScript"); </script> </head> <body> <div>Hello JavaScript</div> </body> </html>
上面使用document.close()关闭文档流了,为什么还是不能够覆盖原来的内容的,很遗憾,文档流是由浏览器创建,无权限手动关闭,document.close()函数只能够关闭由document.open()函数创建的文档流。看下面的代码实例:
<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <title>Document</title> <script type="text/javascript"> function create(){ var newWindow=window.open("","Document","_blank"); newWindow.document.write("Hello JavaScript"); newWindow.document.close(); newWindow.document.write("覆盖后的输出"); } window.onload=function(){ var obt=document.getElementById("bt"); obt.onclick=function(){ create(); } } </script> </head> <body> <div id="print">Hello JavaScript</div> <input type="button" id="bt" value="查看效果"/> </body> </html>
由doucment.open()创建的文档流就可以由document.close()关闭,那么第二个document.write()输出的内容会覆盖掉第一个输出的内容。
异步引用外部JavaScript时,必须先运行document.open()清空文档,然后才能运行document.write(),参数写在body内容的开头。
如果不先运行document.open(),直接运行document.write(),则无效且Chrome有如下提示:
// asyncWrite.js document.open(); document.write('<p>test</p>'); document.close(); <!-- asyncWrite.html --> <!-- 运行前 --> <body> <script src="/UploadFiles/2021-04-02/asyncWrite.js">document.write()也能写入含有script标签的字符串,但是需要转义。写入的script标签中的内容会正常运行。
<!-- 运行前 --> <script> document.write('<script>document.write("<p>test</p>");<\/script>'); </script> <!-- 运行后 --> <script> document.write('<script>document.write("<p>test</p>");<\/script>'); </script> <script>document.write("<p>test</p>");</script> <p>test</p>document.write()可以传入多个参数。
<!-- 运行前 --> <body> <script> document.write('<h2>multiArgument</h2>','<p>test</p>'); </script> </body> <!-- 运行后 --> <body> <script> document.write('<h2>multiArgument</h2>','<p>test</p>'); </script> <h2>multiArgument</h2> <p>test</p> </body>总结
以上所述是小编给大家介绍的JS 中document.write()的用法和清空的原因浅析,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
更新日志
- 国风大师纯音系列《古筝仙子 常静演奏 十指弹一》1CD[FLAC][919MB]
- DasKapital-OneMustHaveChaosInsidetoGiveBirthtoaDancingStar(2024)[24-44.1]WAV
- 王菲.1999-《只爱陌生人》日本东芝版[低速原抓WAV+CUE]
- 张小英1987-金奖金曲第一集[新加坡版][WAV+CUE]
- 鹿晗.2015-重启【海蝶】【FLAC分轨】
- 张芸京.2012-小女孩【金牌大风】【WAV+CUE】
- 余天.2002-雄狮魅力金曲2CD(南方金点系列)【南方】【WAV+CUE】
- 银霞.1982-《结·SAYYESMYBOY》台湾复刻版[WAV+CUE]
- 梁汉文1993-火热动感[引进版][WAV+CUE]
- 梁汉文2008-EDMONDHITS48[香港]3CD[WAV+CUE]
- 国风大师纯音系列《童丽-[渭城曲]》1CD[WAV分轨][593.5MB]
- 国风大师纯音系列《许菱子《烧筝2》古筝曲集》1CD[WAV分轨][1.3GB]
- 王子异《1996 Part.2》[320K/MP3][18.29MB]
- 群星.1994-宝丽金最倾情3CD【宝丽金】【WAV+CUE】
- 乐俊亨.2024-同泪【CharioticMusic】【FLAC分轨】