简体   繁体   English

删除除文本之外的所有 HTML 元素

[英]Remove all HTML elements exept text

whole code整个代码

With Javascript I want to remove all current Elements on the Screen exept the text and it's CSS styles.使用 Javascript,我想删除屏幕上的所有当前元素,除了文本和它的 CSS 样式。 My end goal is that I can essentially exchange the text "Bubble" with "Bounce" and still have the same CSS styling in the end.我的最终目标是我基本上可以将文本“Bubble”与“Bounce”交换,并且最终仍然具有相同的 CSS 样式。 But as I also need to remove ALL Elements from the screen to run the next code I need to clear out the body and CSS entirely.但由于我还需要从屏幕上删除所有元素以运行下一个代码,我需要完全清除正文和 CSS。 This leads to my Problem.这导致了我的问题。 I dont know how to either get the same CSS styling back after clearing it out nor how to exclude the CSS Styling from the clearing.我不知道如何在清除后恢复相同的 CSS 样式,也不知道如何从清除中排除 CSS 样式。 Can anybody help?有人可以帮忙吗?

 document.getElementById("text").innerHTML = "Bubble"; document.addEventListener("click", next); function next() { document.head.innerHTML = " "; document.getElementById("text").innerHTML = "Bounce"; }
 section { width: 100%; height: 100vh; overflow: hidden; background: #1F69FA; display: flex; justify-content: center; align-items: center; flex-direction: column; } content { min-width: 100%; max-width: 100%; margin-left: auto; margin-right: auto; text-align: center; position: absolute; left: 0; right: 0 } section h2 { font-size: 10em; color: #333; margin: 0 auto; text-align: center; font-family: consolas; }
 <section> <div class="content"> <h2 id="text"></h2> </div> </section>

If you really need to call document.head.innerHTML = " ";如果你真的需要调用document.head.innerHTML = " "; this is one way to do it :这是一种方法:

I created a function called add_css() which adds the CSS (which is now stored in a variable) in a style tag in the head of document.我创建了一个名为 add_css() 的函数,它将 CSS(现在存储在一个变量中)添加到文档头部的样式标记中。

Also your CSS had a typo ( content instead of .content )您的 CSS 也有错字( content而不是.content

 document.getElementById("text").innerHTML = "Bubble"; const css_to_keep = `section { width: 100%; height: 100vh; overflow: hidden; background: #1F69FA; display: flex; justify-content: center; align-items: center; flex-direction: column; } .content { min-width: 100%; max-width: 100%; margin-left: auto; margin-right: auto; text-align: center; position: absolute; left: 0; right: 0 } section h2 { font-size: 10em; color: #333; margin: 0 auto; text-align: center; font-family: consolas; }`; document.addEventListener("click", next); add_css(); function next() { document.head.innerHTML = " "; add_css(); document.getElementById("text").innerHTML = "Bounce"; } function add_css(){ const style_elem = document.createElement('style'); document.head.appendChild(style_elem); style_elem.type = 'text/css'; style_elem.appendChild(document.createTextNode(css_to_keep)); }
 <section> <div class="content"> <h2 id="text"></h2> </div> </section>

You don't need to "remove" anything - just replace the innerHTML of the document body with a new updated section.您不需要“删除”任何内容 - 只需将文档正文的 innerHTML 替换为新的更新部分即可。

 // A function that returns a string // (See template strings below) function createSection(text) { return ` <section> <div class="content"> <h2 id="text">${text}</h2> </div> </section> `; } document.body.innerHTML = createSection('Bubble'); document.addEventListener('click', next); // Replace the body HTML with the new section function next() { document.body.innerHTML = createSection('Bounce'); }
 section{width:100%;height:100vh;overflow:hidden;background:#1f69fa;display:flex;justify-content:center;align-items:center;flex-direction:column}content{min-width:100%;max-width:100%;margin-left:auto;margin-right:auto;text-align:center;position:absolute;left:0;right:0}section h2{font-size:10em;color:#333;margin:0 auto;text-align:center;font-family:consolas}

Additional documentation附加文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM