【问题标题】:Javascript popup closing event with asynchronous call带有异步调用的 Javascript 弹出窗口关闭事件
【发布时间】:2017-05-17 14:27:14
【问题描述】:

完整的 Java 脚本:https://jsfiddle.net/v4bwfopm/1/

在当前脚本中

btn.onclick = function() {
    modal.style.display = "block";
}

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

弹出窗口仅在显示警报(“s”)后才关闭。

如何在关闭弹窗后才显示警报?

我添加了 alert("s");在关闭事件之后,在这种情况下,警报会在弹出之前显示。

屏幕截图:

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";
  alert("s");
}
.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;
}
<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>

【问题讨论】:

    标签: javascript popup


    【解决方案1】:

    可以使用setTimeout,所以你的代码可以span.onclick如下

    span.onclick = function() {
        modal.style.display = "none";
        setTimeout(function(){
        alert("s");
        });
    }
    

    【讨论】:

      【解决方案2】:

      只需使用setTimeout(function(){alert("s");},0);

      【讨论】:

        【解决方案3】:

        alert() 方法阻止浏览器更新 CSS,以避免使用 setTimeout() 方法提供延迟。

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

        来自 MDN 文档:

        对话框是模态窗口——它们阻止用户访问程序界面的其余部分,直到对话框关闭。因此,您不应过度使用任何创建对话框(或模式窗口)的函数。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-03-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多