【问题标题】:Show div using button and hide it if click outside the div使用按钮显示 div 并在 div 外部单击时将其隐藏
【发布时间】:2017-12-07 09:41:36
【问题描述】:

我尝试创建一个显示和隐藏 div 的函数,隐藏 div 用户可以使用关闭按钮或单击 div 外部,但问题是如果用户单击 div 之外的元素运行时隐藏 div 的关闭功能首先在显示 div 之前:

html:

$('#close-add').on('click', function() {
  $("#add-shipping-modal").hide();
});

$('#new-shipping').on('click', function() {
  $("#add-shipping-modal").show();
});

$('body').click(function(event) {
  setTimeout(
    if (!$(event.target).closest('#add-shipping-modal').length && !$(event.target).is('#add-shipping-modal')) {
      if ($("#add-shipping-modal").css('display') != 'none') {
        $("#add-shipping-modal").hide();
      }
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="new-shipping'">Open Div</button>
<div id="add-shipping-modal" style="display:none">
  <button id="close-add">Close Div</button>
  <p> Show Me </p>
</div>

当我点击#new-shipping按钮时,隐藏的div不会显示,我猜是因为当我点击#new-shipping按钮时,它先显示div然后触发body点击功能

【问题讨论】:

  • setTimeout 已关闭
  • 不要使用 setTimeout,使用@vipin 回答!
  • 替换 有一个额外的单一的 id属性

标签: javascript jquery html css


【解决方案1】:

W3Schools 已经提供了解决方案。

在这里查看:https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_modal

片段

// 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";
    }
}
/* 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 {
    background-color: #fefefe;
    margin: auto;
    padding: 20px;
    border: 1px solid #888;
    width: 80%;
}

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

.close:hover,
.close:focus {
    color: #000;
    text-decoration: none;
    cursor: pointer;
}
<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">&times;</span>
    <p>Some text in the Modal..</p>
  </div>

</div>

【讨论】:

    【解决方案2】:

    您在新发货后在代码中添加了 ' 额外内容

    <button id="new-shipping'">Open Div</button>
    

    应该是

     <button id="new-shipping">Open Div</button>
    

    https://jsfiddle.net/bntnfhLm/

    另外请使用 preventdefault/stopPropagation 更改 body 点击功能

    $("#add-shipping-modal").click(function(e){
        e.stopPropagation();
    });
    
    $(document).click(function(){
        $("#add-shipping-modal").hide();
    });
    

    【讨论】:

      【解决方案3】:

      试试这个

       $('#new-shipping').on('click', function () {
                  $("#add-shipping-modal").show();
                  return false;
              });
      
              $(document).click(function (event) {
                  $("#add-shipping-modal").hide();
              });
      

      【讨论】:

        【解决方案4】:

        试试这个

        $('#close-add').on('click', function () {
                    $("#add-shipping-modal").hide();
                });
        
                $('#new-shipping').on('click', function () {
                    $("#add-shipping-modal").show();
                    return false;
                });
                $("#add-shipping-modal").click(function () { return false; });
        
                $(document).click(function (event) {
                    $("#add-shipping-modal").hide();
                });
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-02-23
          • 2021-06-14
          • 1970-01-01
          • 2013-01-16
          • 2016-05-21
          • 1970-01-01
          • 2013-04-18
          • 1970-01-01
          相关资源
          最近更新 更多