首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Javascript:避免多个弹出框/检查div是否为空

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

Stack Overflow用户
提问于 2017-11-18 13:38:20
回答 2查看 118关注 0票数 0

我创建了一个弹出框,当用户单击按钮时会出现该弹出框。目前,我可以让框出现,但当用户关闭它并重新打开它时,代码再次运行,另一个框堆叠在旧框的顶部。

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

对菜鸟笨蛋有什么建议吗?提前感谢所有回复。

代码语言:javascript
复制
<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>
EN

回答 2

Stack Overflow用户

发布于 2017-11-18 15:04:38

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

代码语言:javascript
复制
<!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>

票数 0
EN

Stack Overflow用户

发布于 2017-11-18 15:31:29

实际上,我只需在代码块出现后添加box.innerHTML = ''就可以解决这个问题,这样它就可以从头开始工作了。我试图变得更优雅一些,并尝试使用If/Else语句,但最终它似乎起作用了。非常感谢所有回复/评论的人,你们都很棒!

代码语言:javascript
复制
<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";}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47362911

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档