首页 > 解决方案 > 关闭弹出窗口使其从 DOM 中消失并启用它

问题描述

我有这个按钮显示一个仅由 html 和 css 制作的弹出窗口,我想让弹出窗口在我关闭它时从 DOM 中消失,单击弹出窗口上的“从 dom 删除”按钮,但我没有找到与“removeChild()”方法相反,一旦它关闭就启用它,我确实尝试了“appendChild()”,这对我不起作用。谢谢你。

  function del() {
var elem = document.querySelector('#popup1');

elem.parentNode.removeChild(elem);
}
h1 {
  text-align: center;
  font-family: Tahoma, Arial, sans-serif;
  color: #06D85F;
  margin: 80px 0;
}

.box {
  width: 40%;
  margin: 0 auto;
  background: rgba(255,255,255,0.2);
  padding: 35px;
  border: 2px solid #fff;
  border-radius: 20px/50px;
  background-clip: padding-box;
  text-align: center;
}

.button {
  font-size: 1em;
  padding: 10px;
  color: #000;
  border: 2px solid #06D85F;
  border-radius: 20px/50px;
  text-decoration: none;
  cursor: pointer;
  transition: all 0.3s ease-out;
}
.button:hover {
  background: #06D85F;
}

.overlay {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(0, 0, 0, 0.7);
  transition: opacity 500ms;
  visibility: hidden;
  opacity: 0;
}
.overlay:target {
  visibility: visible;
  opacity: 1;
}

.popup {
  margin: 70px auto;
  padding: 20px;
  background: #fff;
  border-radius: 5px;
  width: 30%;
  position: relative;
  transition: all 5s ease-in-out;
}

.popup h2 {
  margin-top: 0;
  color: #333;
  font-family: Tahoma, Arial, sans-serif;
}
.popup .close {
  position: absolute;
  top: 20px;
  right: 30px;
  transition: all 200ms;
  font-size: 30px;
  font-weight: bold;
  text-decoration: none;
  color: #333;
}
.popup .close:hover {
  color: #06D85F;
}
.popup .content {
  max-height: 30%;
  overflow: auto;
}

@media screen and (max-width: 700px){
  .box{
    width: 70%;
  }
  .popup{
    width: 70%;
  }
}
<div class="box">
  <a class="button"  href="#popup1">Let me Pop up</a>
 
</div>

<div id="popup1" class="overlay">
	<div class="popup">
		<h2>Title</h2>
		<a class="close"  href="#">&times;</a>
		<div class="content">
      Text
      <input type="submit" value="delete from dom" onclick=del() />
		</div>
	</div>
</div>

标签: javascripthtmlcss

解决方案


如果您真的想从 DOM 中删除弹出窗口,则必须保留对它的引用(在变量中)。

您还必须保存父级(.parentNode显然在元素被删除且没有父级时不起作用):

var elem = document.querySelector('#popup1');
var parent = elem.parentNode

function del() {
  parent.removeChild(elem);
}

function add() {
  parent.appendChild(elem)
}
h1 {
  text-align: center;
  font-family: Tahoma, Arial, sans-serif;
  color: #06D85F;
  margin: 80px 0;
}

.box {
  width: 40%;
  margin: 0 auto;
  background: rgba(255,255,255,0.2);
  padding: 35px;
  border: 2px solid #fff;
  border-radius: 20px/50px;
  background-clip: padding-box;
  text-align: center;
}

.button {
  font-size: 1em;
  padding: 10px;
  color: #000;
  border: 2px solid #06D85F;
  border-radius: 20px/50px;
  text-decoration: none;
  cursor: pointer;
  transition: all 0.3s ease-out;
}

.button:hover {
  background: #06D85F;
}

.overlay {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(0, 0, 0, 0.7);
  transition: opacity 500ms;
  visibility: hidden;
  opacity: 0;
}

.overlay:target {
  visibility: visible;
  opacity: 1;
}

.popup {
  margin: 70px auto;
  padding: 20px;
  background: #fff;
  border-radius: 5px;
  width: 30%;
  position: relative;
  transition: all 5s ease-in-out;
}

.popup h2 {
  margin-top: 0;
  color: #333;
  font-family: Tahoma, Arial, sans-serif;
}

.popup .close {
  position: absolute;
  top: 20px;
  right: 30px;
  transition: all 200ms;
  font-size: 30px;
  font-weight: bold;
  text-decoration: none;
  color: #333;
}

.popup .close:hover {
  color: #06D85F;
}

.popup .content {
  max-height: 30%;
  overflow: auto;
}

@media screen and (max-width: 700px) {
  .box {
    width: 70%;
  }
  .popup {
    width: 70%;
  }
}
<div class="box">
  <a class="button" href="#popup1" onclick="add()">Let me Pop up</a>

</div>

<div id="popup1" class="overlay">
  <div class="popup">
    <h2>Title</h2>
    <a class="close" href="#">&times;</a>
    <div class="content">
      Text
      <input type="submit" value="delete from dom" onclick="del()" />
    </div>
  </div>
</div>


推荐阅读