javascript - 模态不会在按钮和/或窗口单击事件时关闭
问题描述
我在一个不起作用的模式旁边有一个关闭按钮,我正在努力弄清楚(尽管其他票证中提供了所有资源)。当我单击模态框外的任何位置时,模态框也不会关闭。
// Get the modal
var modal = document.getElementById("myModal");
// Get the image and insert it inside the modal - use its "alt" text as a caption
var img = document.getElementById("myImg");
var modalImg = document.getElementById("img01");
var captionText = document.getElementById("caption");
img.onclick = function() {
modal.style.display = "block";
modalImg.src = this.src;
captionText.innerHTML = this.alt;
}
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("closebtn")[0];
// 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 class="card-header">
<!-- Trigger the Modal -->
<img id="myImg" src="pathtomyimage" alt="mycaptiontext" style="width:100%;max-width:600px">
<!-- The Modal -->
<div id="myModal" class="modal">
<!-- The Close Button -->
<span class="closebtn">×</span>
<!-- Modal Content (The Image) -->
<img class="modal-content" id="img01">
<!-- Modal Caption (Image Text) -->
<div id="caption"></div>
</div>
</div>
解决方案
使发布的代码正常工作的两个更改:
在打开模式的单击处理程序中停止事件传播。
- 如果没有停止,则打开模态的单击会到达窗口单击处理程序并在打开模态后立即关闭它。
解决窗口单击处理程序中的拼写错误正在检查
(event.target == modal)
而不是检查的问题(event.target != modal)
,但如果用户单击模式元素本身内的元素,则简单地测试会(event.target != modal)
返回误报。代码片段解决了
element.closest
用于检测是否event.target
具有modal
类或具有类的祖先元素的组合问题modal
。
"use strict";
// Get the modal
var modal = document.getElementById("myModal");
// Get the image and insert it inside the modal - use its "alt" text as a caption
var img = document.getElementById("myImg");
var modalImg = document.getElementById("img01");
var captionText = document.getElementById("caption");
img.onclick = function( event){
modal.style.display = "block";
modalImg.src = this.src;
captionText.innerHTML = this.alt;
event.stopPropagation(); // prevent event reaching window.onclick
}
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("closebtn")[0];
// When the user clicks on <span> (x), close the modal
span.onclick = function(){
modal.style.display = "none";
}
/*** updated window.onclick hander ***/
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event){
let modalContainer = event.target.closest(".modal");
if( modalContainer === null && modal.style.display == "block") {
modal.style.display = "none";
}
}
img {
min-height: 4rem;
min-width: 4rem;
border: medium solid blue;
}
.closebtn {
font-size:200%;
color:white;
background-color: red;
}
.modal {
background-color: yellow;
display: none;
}
<div class="card-header">
<!-- Trigger the Modal -->
<img id="myImg" src="pathtomyimage" alt="mycaptiontext" style="width:100%;max-width:600px">
<!-- The Modal -->
<div id="myModal" class="modal">
<!-- The Close Button -->
<span class="closebtn">×</span>
<!-- Modal Content (The Image) -->
<img class="modal-content" id="img01">
<!-- Modal Caption (Image Text) -->
<div id="caption"></div>
</div>
</div>
推荐阅读
- string - Perl:从文件读取后如何检查数组列中单词的最大宽度?
- android - 如何在移动开发者播放器(flutter/ios/android/reactnative)中播放 Prime 或 Netflix 视频?
- node.js - 在 reactjs 中进行服务器端调试
- html - 如何使用css反转动画?
- mysql - Mysql 连接应该从表 A 中返回所有不在表 B 中的 AND 表 B 的 where 子句
- typescript - 如何在 vue.js 中绑定类?
- python - 超过最大重试次数
- google-sheets - 匿名共享 Google 表格
- android-studio - 解决错误“app:mergeDebugResources”和“文件名必须以 XML 结尾”
- nginx - Kubernetes Ingress Nginx 服务:AWS API Gateway/CloudFront 返回 403