document.write详细说明

本文详细解释了document.write()函数的工作原理及其潜在的问题,包括在不同场景下如何正确使用该函数,尤其是在事件处理程序中避免覆盖现有文档内容的方法。

document.write()用来向文档输出,函数很简单但是有时候也会出现使用错误,例如在事件中输出,它会覆盖当前文档输出内容。原因是:document.write只能在文档解析过程中输出,倘若在文档完全载入则覆盖当前文档和它所包含的脚本。

所以对于<script defer></script>来说会就会产生该种情况,因为defer告诉浏览器,将脚本调用延迟在文档加载完成。

 

document.open()用于打开一个新文档,如果文档已经关闭或者加载完,未在document.write()之前调用document.open()方法则默认会创建新文档,这就是为什么在事件中使用document.write()方法会使当前文档的内容和脚本全部丢弃。

document.close()关闭文档流

doument.write()可以又多个参数,例如document.write(s,b,d);

 

上面说了document.write()在事件时输出文档会有文档,如果我要输出的话应该怎么办呢,我们可以用innerhtml或者dom 2中的文档creatElement,apendpchild等方法。

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <style> #time{ width: 500px; height: 50px; background-color: white; } </style> </head> <body> <div id="time"> </div> </body> <script> var date = new Date() document.write(date.getFullYear()+"年") document.write(date.getMonth()+1+"月") document.write(date.getDate()+"日") document.write(date.getHours()+"时") document.write(date.toLocaleDateString()) document.write(date.toLocaleTimeString()) document.write(date.getDay()) var day = date.getDay() var arr =["日","一","二","三","四","五","六"] document.write("今天是星期"+arr[day]) function getdate(){ var date = new Date() var showTime = document.getElementById("time") var date1 = date.toLocaleDateString() var time1 = date.toLocaleTimeString() showTime.innerHTML="<br>"+date1+" "+time1 } getdate() setInterval('getdate()',500) </script> </html> 帮我修改<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <style> #time{ width: 500px; height: 50px; background-color: white; } </style> </head> <body> <div id="time"> </div> </body> <script> var date = new Date() document.write(date.getFullYear()+"年") document.write(date.getMonth()+1+"月") document.write(date.getDate()+"日") document.write(date.getHours()+"时") document.write(date.toLocaleDateString()) document.write(date.toLocaleTimeString()) document.write(date.getDay()) var day = date.getDay() var arr =["日","一","二","三","四","五","六"] document.write("今天是星期"+arr[day]) function getdate(){ var date = new Date() var showTime = document.getElementById("time") var date1 = date.toLocaleDateString() var time1 = date.toLocaleTimeString() showTime.innerHTML="<br>"+date1+" "+time1 } getdate() setInterval('getdate()',500) </script> </html><!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <style> #time{ width: 500px; height: 50px; background-color: white; } </style> </head> <body> <div id="time"> </div> </body> <script> var date = new Date() document.write(date.getFullYear()+"年") document.write(date.getMonth()+1+"月") document.write(date.getDate()+"日") document.write(date.getHours()+"时") document.write(date.toLocaleDateString()) document.write(date.toLocaleTimeString()) document.write(date.getDay()) var day = date.getDay() var arr =["日","一","二","三","四","五","六"] document.write("今天是星期"+arr[day]) function getdate(){ var date = new Date() var showTime = document.getElementById("time") var date1 = date.toLocaleDateString() var time1 = date.toLocaleTimeString() showTime.innerHTML="<br>"+date1+" "+time1 } getdate() setInterval('getdate()',500) </script> </html> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <style> #time{ width: 500px; height: 50px; background-color: white; } </style> </head> <body> <div id="time"> </div> </body> <script> var date = new Date() document.write(date.getFullYear()+"年") document.write(date.getMonth()+1+"月") document.write(date.getDate()+"日") document.write(date.getHours()+"时") document.write(date.toLocaleDateString()) document.write(date.toLocaleTimeString()) document.write(date.getDay()) var day = date.getDay() var arr =["日","一","二","三","四","五","六"] document.write("今天是星期"+arr[day]) function getdate(){ var date = new Date() var showTime = document.getElementById("time") var date1 = date.toLocaleDateString() var time1 = date.toLocaleTimeString() showTime.innerHTML="<br>"+date1+" "+time1 } getdate() setInterval('getdate()',500) </script> </html><!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <style> #time{ width: 500px; height: 50px; background-color: white; } </style> </head> <body> <div id="time"> </div> </body> <script> var date = new Date() document.write(date.getFullYear()+"年") document.write(date.getMonth()+1+"月") document.write(date.getDate()+"日") document.write(date.getHours()+"时") document.write(date.toLocaleDateString()) document.write(date.toLocaleTimeString()) document.write(date.getDay()) var day = date.getDay() var arr =["日","一","二","三","四","五","六"] document.write("今天是星期"+arr[day]) function getdate(){ var date = new Date() var showTime = document.getElementById("time") var date1 = date.toLocaleDateString() var time1 = date.toLocaleTimeString() showTime.innerHTML="<br>"+date1+" "+time1 } getdate() setInterval('getdate()',500) </script> </html><!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <style> #time{ width: 500px; height: 50px; background-color: white; } </style> </head> <body> <div id="time"> </div> </body> <script> var date = new Date() document.write(date.getFullYear()+"年") document.write(date.getMonth()+1+"月") document.write(date.getDate()+"日") document.write(date.getHours()+"时") document.write(date.toLocaleDateString()) document.write(date.toLocaleTimeString()) document.write(date.getDay()) var day = date.getDay() var arr =["日","一","二","三","四","五","六"] document.write("今天是星期"+arr[day]) function getdate(){ var date = new Date() var showTime = document.getElementById("time") var date1 = date.toLocaleDateString() var time1 = date.toLocaleTimeString() showTime.innerHTML="<br>"+date1+" "+time1 } getdate() setInterval('getdate()',500) </script> </html>帮我修改这个代码成这样 2025年5月10日 19:20:10 星期三
10-24
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值