document.write一次重置主体 [英] document.write resets body once

查看:97
本文介绍了document.write一次重置主体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在加载正文后(通过onclick事件)使用document.write时,它将覆盖整个正文.我想要这是我想要实现的目标.但是,当我再次执行此操作时,它只是添加到先前的内容中.为什么它第一次覆盖所有内容,而仅在下一次添加?

解决方案

如果未打开文档供编写,则调用document.write将自动调用document.open.

第一次调用document.write时,浏览器已加载文档并因此将其关闭以进行写入.这会调用document.open,它会擦除​​现有文档并创建一个新文档以写入.

由于浏览器未加载此内容,因此它不会自动关闭以进行写入,因此在再次调用docment.write时将附加内容.如果要清除内容,可以显式调用document.closedocument.open.

有关更多详细信息,请参见 https://developer.mozilla.org/en/document.write ./p>

When I used document.write after loading the body (via onclick event), it overwrites the entire body. I want this for what I'm trying to achieve. However, when I do it again, it simply adds on to the previous content. Why does it overwrite everything the first time and only add on the next?

解决方案

If the document isn't open for writing, then calling document.write will automatically call document.open.

The first time you call document.write, the browser has loaded the document and thus closed it for writing. This calls document.open which wipes out the existing document and creates a new one to write into.

Since the browser isn't loading this, it doesn't automatically close it for writing, so the content is appended when you call docment.write again. You can explicitly call document.close and document.open if you want to wipe out the content.

See https://developer.mozilla.org/en/document.write for more details.

这篇关于document.write一次重置主体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆