最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to clear all that's written on an HTML document? - Stack Overflow

programmeradmin2浏览0评论

How do I clear off all that was written in a document? Is there a DOM JavaScript API that will erase all that's in the document object's buffer; an equivalent of the server-side Response.Clear()?

I am just practicing JavaScript.

How do I clear off all that was written in a document? Is there a DOM JavaScript API that will erase all that's in the document object's buffer; an equivalent of the server-side Response.Clear()?

I am just practicing JavaScript.

Share Improve this question asked Jul 30, 2015 at 18:20 Water Cooler v2Water Cooler v2 33.9k63 gold badges183 silver badges365 bronze badges 3
  • JavaScript DOM API – crush Commented Jul 30, 2015 at 18:23
  • 1 document.write(''); would do... – ᔕᖺᘎᕊ Commented Jul 30, 2015 at 18:23
  • What do you mean by "buffer"? – Amit Commented Jul 30, 2015 at 18:23
Add a ment  | 

3 Answers 3

Reset to default 5

here's an alternative that doesn't require querying:

document.documentElement.remove(); 

You can remove the root (html) element using:

document.querySelector('html').remove();

or the jQuery equivalent if you have a very old browser:

$('html').remove();

What do you mean by “document object's buffer”? The actual visible content is usually contained in the body, so you can empty it:

document.body.innerHTML = null;

There is no buffer like in server-side preprocessor languages, because the DOM document is entirely client-side rendered.

You can even wipe all the document's descendants, clearing the <html> element:

document.getElementsByTagName('html').item(0).innerHTML = null;

Also, if you don't like innerHTML, you can also use textContent:

document.head.textContent = null;

This is because, quoting MDN:

Setting this property on a node removes all of its children and replaces them with a single text node with the given value.

发布评论

评论列表(0)

  1. 暂无评论