我使用此代码创建基本模式:https://codepen.io/paallaire/project/editor/ZBajYk/
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
<link rel="stylesheet" href="main.css">
</head>
<body>
<div class="c-modal-request js-modal-request-close">
<div class="c-modal-request__content">
<a href="#" class="c-modal-request__btn-close js-modal-request-close">Close ME</a>
<h1>HTML Ipsum Presents</h1>
<p><strong>Pellentesque habitant morbi tristique</strong> senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. <em>Aenean ultricies mi vitae est.</em> Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, <code>commodo vitae</code>, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci,
sagittis tempus lacus enim ac dui. <a href="#">Donec non enim</a> in turpis pulvinar facilisis. Ut felis.</p>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="main.js"></script>
</body>
</html>
目标: 如果我点击&#34;红色&#34;区域或我点击按钮&#34;关闭我&#34;
结果=模态关闭(对于此示例,我使用了jquery.hide())
但我试着理解为什么当我点击白色区域(模态)模态关闭时。
答案 0 :(得分:1)
问题是:你的div 模态有js-modal-request-close
类,它与close me
的类相同。为此,div关闭时点击里面
要解决这个问题,你需要
<强> 1 强>
<div class="c-modal-request js-modal-request-close">
应该是
<div class="c-modal-request">
<强> 2 强>
e.stopPropagation();
防止事件冒泡DOM树,防止任何事件 家长处理人员被告知该事件。
你可以像这样使用它
$(document).ready(function() {
console.log("test");
console.log($);
$(".js-modal-request-close , .c-modal-request").on("click", function(e) {
e.preventDefault();
$(".c-modal-request").hide();
});
$(".c-modal-request__content").on('click' , function(e){
e.stopPropagation();
});
});
&#13;
.c-modal-request {
position: fixed;
top: 0;
left: 0;
z-index: 300;
width: 100%;
height: 100%;
background-color: red;
}
.c-modal-request__content {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
padding: 60px;
max-width: 720px;
width: 100%;
overflow-y: auto;
background-color: #fff;
position: relative;
z-index: 400;
}
.c-modal-request__btn-close {
color: #000;
background-color: yellow;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="c-modal-request">
<div class="c-modal-request__content">
<a href="#" class="c-modal-request__btn-close js-modal-request-close">Close ME</a>
<h1>HTML Ipsum Presents</h1>
<p><strong>Pellentesque habitant morbi tristique</strong> senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. <em>Aenean ultricies mi vitae est.</em> Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, <code>commodo vitae</code>, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci,
sagittis tempus lacus enim ac dui. <a href="#">Donec non enim</a> in turpis pulvinar facilisis. Ut felis.</p>
</div>
</div>
&#13;
其他:关于在点击外部时关闭div,您需要查看Use jQuery to hide a DIV when the user clicks outside of it
答案 1 :(得分:0)
你必须检查你点击的元素是真的.js-modal-request-close
还是后代:
$(".js-modal-request-close").on("click", function(e) {
if (e.target !== this)
return;
$(".c-modal-request").hide();
});