击中[x]时如何使弹出窗口关闭

时间:2019-11-22 17:11:09

标签: javascript html

我一直在尝试使我的弹出窗口关闭,但没有成功。我之所以如此困惑,是因为我的模式在定位时就关闭了,我的模式是实际弹出文本周围的空间,所以如果我单击屏幕上的任何其他位置,则除文本之外,屏幕上除文本之外的任何位置都会关闭,但是然后我对跨度使用类似的javascript代码,其中包含x符号但不会将其关闭(javascript来自(w3)

// 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";
  }
}
<div id="myModal" class="modal">
  <!-- Modal content -->
  <div class="modal-content">
    <span class="close">&times;</span>
    <p>
      <b>Rules</b>
      <br>
      <b>1.</b>
      <b>blahblahblahblah</b>
    </p>
  </div>
</div>

和上面的是我的html,其中包含id和类,我只想知道为什么我的弹出窗口在我按下x后没有关闭,但是当我在外面的其他任何地方命中时却弹出了也有

1 个答案:

答案 0 :(得分:-1)

我有两个“封闭式”班级,在我将该班级更改为另一个名称后,它们都可以正常工作。谢谢你德祥