简体   繁体   English

完全删除/删除div并显示另一个(切换)

[英]Completely remove / delete a div and show another (toggle)

I'm trying to make a div completely delete itself not just set the style to none for example, I am wondering if it's possible to completely remove and show another, but toggle so that it can be retrieved still. 我正在尝试使div完全删除自身,而不仅仅是将样式设置为none,我想知道是否有可能完全删除并显示另一个样式,而是切换以使其仍可检索。 Is this possible using html. 是否可以使用html。 Would loading it off another file be the only solution? 将其从另一个文件加载是唯一的解决方案吗?

At the moment I'm redirecting the user depending on their select to a new page but I'm wanting it all on the same page ideally but that it updates completely. 目前,我正在根据用户的选择将用户重定向到新页面,但是理想情况下,我希望所有页面都在同一页面上,但是它会完全更新。

function showDiv(elem) {
   if (elem.value == 19) {
     // show div 1
     // delete div 2 completely
   } else if(elem.value == 20) {
     // show div 2
     // delete div 1 completely
   }
}

If you want to do this on client side, you can use template approach: 如果要在客户端执行此操作,可以使用模板方法:

 function showDiv(elem) { var value = elem.value; var templateId = 'template' + value; var containerId = 'main-container'; var template = document.getElementById(templateId); var container = document.getElementById(containerId); if (template && container) { container.innerHTML = template.textContent; } } showDiv({value: 19}); 
 <select onchange="showDiv(this)"> <option>19</option> <option>20</option> </select> <div id="main-container"></div> <script type="text/template" id="template19"> <div>Content 19</div> </script> <script type="text/template" id="template20"> <div>Content 20</div> </script> 

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

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