Javascript:避免多个弹出框/检查div是否为空

时间:2017-11-18 05:38:20

标签: javascript html css if-statement is-empty

我创建了一个弹出框,当用户单击按钮时会出现该框。目前我能够显示该框,但是当用户关闭它并重新打开它时,代码再次运行,另一个框堆叠在旧框之上。

我想做的就是这样做,无论用户点击框多少次,它只显示一个框...我的想法可能是检查它是否为空,类似于How do I check if an HTML element is empty using jQuery?但是没有到目前为止工作。

对noob白痴的任何建议?提前感谢所有回复。

<button onclick="popup()">text</button>

<script>
var box = document.getElementById('popupbox');

function popup(){
  box.style.display = "block";
  var content = document.createElement('div');
  content.className = 'boxstyle';
  content.innerHTML = ' ...a handful of tags...';
  box.appendChild(content);}

function exit(){
box.style.display = "none";}
</script>

2 个答案:

答案 0 :(得分:0)

您可以使用模式创建弹出窗口,如下所示。我刚刚复制了代码from here

&#13;
&#13;
<!DOCTYPE html>
<html>
<head>
<style>
/* The Modal (background) */
.modal {
    display: none; /* Hidden by default */
    position: fixed; /* Stay in place */
    z-index: 1; /* Sit on top */
    padding-top: 100px; /* Location of the box */
    left: 0;
    top: 0;
    width: 100%; /* Full width */
    height: 100%; /* Full height */
    overflow: auto; /* Enable scroll if needed */
    background-color: rgb(0,0,0); /* Fallback color */
    background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}

/* Modal Content */
.modal-content {
    background-color: #fefefe;
    margin: auto;
    padding: 20px;
    border: 1px solid #888;
    width: 80%;
}

/* The Close Button */
.close {
    color: #aaaaaa;
    float: right;
    font-size: 28px;
    font-weight: bold;
}

.close:hover,
.close:focus {
    color: #000;
    text-decoration: none;
    cursor: pointer;
}
</style>
</head>
<body>

<h2>Modal Example</h2>

<!-- Trigger/Open The Modal -->
<button id="myBtn">Open Modal</button>

<!-- The Modal -->
<div id="myModal" class="modal">

  <!-- Modal content -->
  <div class="modal-content">
    <span class="close">&times;</span>
    <p>Some text in the Modal..</p>
  </div>

</div>

<script>
// Get the modal
var modal = document.getElementById('myModal');

// Get the button that opens the modal
var btn = document.getElementById("myBtn");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks the button, open the modal 
btn.onclick = function() {
    modal.style.display = "block";
}

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
    modal.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
    if (event.target == modal) {
        modal.style.display = "none";
    }
}
</script>

</body>
</html>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

我实际上设法通过在块出现之后立即向box.innerHTML = ''折腾来解决它,以便它有一个干净的平板可以使用。我试图更优雅一点,并试图使用If / Else语句,但最终似乎有效。非常感谢所有回答/评论的人,你们都很棒!

<button onclick="popup()">text</button>

<script>
var box = document.getElementById('popupbox');

function popup(){
  box.style.display = "block";
  box.innerHTML = '';
  var content = document.createElement('div');
  content.className = 'boxstyle';
  content.innerHTML = ' ...a handful of tags...';
  box.appendChild(content);}

function exit(){
  box.style.display = "none";}