document.write 重置 body 一次
当我在加载正文后使用 document.write 时(通过 onclick 事件),它会覆盖整个正文。我想要这个是为了我想要实现的目标。然而,当我再次这样做时,它只是添加到之前的内容中。为什么它第一次会覆盖所有内容并只在下一次添加?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果文档未打开以供写入,则调用
document.write
将自动调用document.open
。第一次调用
document.write
时,浏览器已加载文档并关闭它以进行写入。这会调用document.open
来清除现有文档并创建一个新文档进行写入。由于浏览器不会加载此内容,因此它不会自动关闭它以进行写入,因此当您再次调用
docment.write
时会附加内容。如果您想清除内容,可以显式调用document.close
和document.open
。有关更多详细信息,请参阅 https://developer.mozilla.org/en/document.write。
If the document isn't open for writing, then calling
document.write
will automatically calldocument.open
.The first time you call
document.write
, the browser has loaded the document and thus closed it for writing. This callsdocument.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 calldocument.close
anddocument.open
if you want to wipe out the content.See https://developer.mozilla.org/en/document.write for more details.
当您使用
document.write
时,它会直接写入页面/正文。如果您想写入某个特定元素,可以使用如下语法:When you use
document.write
, it writes directly to the page/body. If you want to write to some specific element, you can use syntax such as this: