admin管理员组文章数量:1406937
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?
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?
Share Improve this question edited Jan 3, 2012 at 20:29 Josh Darnell 11.5k9 gold badges39 silver badges66 bronze badges asked Dec 22, 2010 at 17:32 AnonymousAnonymous 1,8937 gold badges23 silver badges29 bronze badges2 Answers
Reset to default 11If 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/en/document.write for more details.
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:
document.getElementById('elementID').innerHTML = 'your string';
本文标签: javascriptdocumentwrite resets body onceStack Overflow
版权声明:本文标题:javascript - document.write resets body once - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744972474a2635314.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论