簡體   English   中英

當用戶在其外部單擊時關閉模式,為什么有==而不是!=?

[英]Close modal when user clicks outside of it, why is there == and not !=?

我從這里看了一些代碼: https : //www.w3schools.com/howto/howto_css_modals.asp ,但是我想理解為什么下面的代碼中==而不是!=:

// Get the modal
var modal = document.getElementById('yourModalId');

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
    if (event.target == modal) {
        modal.style.display = "none";
    }
}

單擊模式的外部時,我們想關閉窗口。 為什么那里有一個等號呢?

看起來是因為modal是背景,而modal-content是您認為應該是模態的東西。

請參閱此處的工作示例,並注意以下CSS塊:

/* 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%;
}

根據提供的鏈接, modalmodal的燈箱。 (透明背景)因此,當用戶單擊時,請關閉模式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM