Javascript modal 需要点击两次才能打开

标签 javascript html css modal-dialog

代码应在单击按钮后打开模式窗口,但第一次打开它时,需要单击两次按钮才能打开它。 在从 W3Schools 复制一些代码并修改它以使其适合我的 JS 文件后,我遇到了这个问题。

HTML

<h2>Modal Example</h2>

<!-- Trigger/Open The Modal -->
<button id="myBtn" onclick="modalFunction()">Open Modal</button>

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

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

</div>

Java 脚本

function modalFunction() {
// 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";
    }
}
}

最佳答案

如果您将事件加载处理程序移到函数之外,它将按预期工作。

这里我只是删除了函数和内联脚本处理程序。

我还将 onlick 更改为较新的 addEventListener,这里有一个很好的答案来解释它们的区别:addEventListener vs onclick

请注意,脚本需要在页面加载时运行,而不是之前

// Make sure DOM is loaded before access any of its elements
window.addEventListener('load', function() {

  // Get the modal
  var mdl = document.getElementById('myModal');

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

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

  // When the user clicks the button, open the modal 
  btn.addEventListener('click', function(ev) {
    mdl.style.display = "block";
    // Prevent event bubbling (so in this case the window listener
    // won't catch the event and simply close it immediately)
    ev.stopPropagation();
  });

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

  // When the user clicks anywhere outside of the modal, close it
  window.addEventListener('click', function(ev) {
    if (!mdl.contains(ev.target)) {
      mdl.style.display = "none";
    }
  });
});
.modal {
  position: relative;
  display: none;
  border: 1px solid black;
}
.close {
  position: absolute;
  display: block;
  color: white;
  background: red;
  padding: 3px 5px;
  right: 0;
  top: 0;
}
<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"> X </span>
    <p>Some text in the Modal..</p>
  </div>

</div>

关于Javascript modal 需要点击两次才能打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40896211/

相关文章:

javascript - 使用 mechanize 勾选复选框

html - div 元素实现的 css3 样式,如本网站中的标签

html - 在 <li> 中填充

javascript - 什么是 ES6 生成器,如何在 node.js 中使用它们?

javascript - 寻找可用于循环显示 3 种状态的 JavaScript 开关控件

javascript - Jquery 图像 slider 上一个按钮

html - 将 2 个 div 并排放置在嵌入 iframe 的方形容器中

HTML Inline Flex 不适应主类

javascript - 如何在 meteor 1.4.1.1 中使用 intro.js 形式的 npm 包?

javascript - 我如何给这个元素惯性/动量?