首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在w3schools示例中,当用户在模式窗口之外单击时,如何使用jquery关闭模式窗口?

问在w3schools示例中,当用户在模式窗口之外单击时,如何使用jquery关闭模式窗口?
EN

Stack Overflow用户
提问于 2016-08-24 17:10:41
回答 2查看 1.4K关注 0票数 3

链接:http://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_modal2

现有代码:

代码语言:javascript
复制
window.onclick = function(event) {
    if (event.target == modal) {
        modal.style.display = "none";
    }
}

根据Jquery更改:

代码语言:javascript
复制
$(window).on('click', function(event){
    if(event.target == $('#myModal')){
        $('#myModal').css({display: "none"});
    }
});

事件正在激发,但条件未传递。你能建议一下做这件事的方法吗?

关闭模式似乎也有同样的问题,但我的问题是当用户点击模式的外部区域时,如何关闭模式窗口。How to close a modal by clicking outside the modal window?包含当用户点击关闭按钮时关闭模式窗口。谢谢你。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-08-24 17:17:08

尝试匹配id而不是整个对象,例如,

代码语言:javascript
复制
$(window).on('click', function(event){
    if(event.target.id == 'myModal'){
        $('#myModal').css({display: "none"});
    }
});

​

代码语言:javascript
复制
// Get the button that opens the modal
var btn = document.getElementById("myBtn");
var modal = document.getElementById('myModal');

// 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";
    }
}*/
$(window).on('click', function(event){
    if(event.target.id == 'myModal'){
        $('#myModal').css({display: "none"});
    }
});
代码语言:javascript
复制
/* The Modal (background) */
.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 {
    position: relative;
    background-color: #fefefe;
    margin: auto;
    padding: 0;
    border: 1px solid #888;
    width: 80%;
    box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
    -webkit-animation-name: animatetop;
    -webkit-animation-duration: 0.4s;
    animation-name: animatetop;
    animation-duration: 0.4s
}

/* Add Animation */
@-webkit-keyframes animatetop {
    from {top:-300px; opacity:0}
    to {top:0; opacity:1}
}

@keyframes animatetop {
    from {top:-300px; opacity:0}
    to {top:0; opacity:1}
}

/* The Close Button */
.close {
    color: white;
    float: right;
    font-size: 28px;
    font-weight: bold;
}

.close:hover,
.close:focus {
    color: #000;
    text-decoration: none;
    cursor: pointer;
}

.modal-header {
    padding: 2px 16px;
    background-color: #5cb85c;
    color: white;
}

.modal-body {padding: 2px 16px;}

.modal-footer {
    padding: 2px 16px;
    background-color: #5cb85c;
    color: white;
}
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<h2>Animated Modal with Header and Footer</h2>

<!-- Trigger/Open The Modal -->
<button id="myBtn">Open Modal</button>
<div id="myModal" class="modal">

  <!-- Modal content -->
  <div class="modal-content">
    <div class="modal-header">
      <span class="close">×</span>
      <h2>Modal Header</h2>
    </div>
    <div class="modal-body">
      <p>Some text in the Modal Body</p>
      <p>Some other text...</p>
    </div>
    <div class="modal-footer">
      <h3>Modal Footer</h3>
    </div>
  </div>

</div>

​

票数 8
EN

Stack Overflow用户

发布于 2016-08-24 17:50:03

我正在使用函数得到答案

代码语言:javascript
复制
$(window).on('click', function(event){
if(event.target.nodeName != 'BUTTON'
&& $('#myModal').css('display') == "block"){
    $('#myModal').css({display: "none"});
}
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39119052

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档