簡體   English   中英

如何在我的模態上創建第二個關閉按鈕(除了左上角x)

[英]How do I create a second close button on my modal (in addition to the top left x)

我想在模式底部添加另一個“取消”按鈕,以及關閉模態的span選項。 不知道從哪里開始請告知一些選項。 謝謝

<!-- The Offer Modal-->
<!-- Trigger/Open The Modal -->
<button id="makeOfferBtn">Make an Offer</button>
<!-- The Offer Modal content -->
<div id="myOfferModal" class="modal">
    <!-- Modal content -->
    <div class="modal-content">
        <span class="close">&times;</span>
    </div>
</div>


<script>
// Get the modal
var modal = document.getElementById('myOfferModal');

// Get the button that opens the modal
var btn = document.getElementById("makeOfferBtn");

// 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";
    }
}
</script>

這是一個快速而又臟的示例,可以使用定義了.close類的任意數量的按鈕:

// Selecting all nodes that are `.close`
var closeBtns = document.querySelectorAll('.close');

// Iterating over on all of them and 
// attaching handlers for the 'click' event. 
for (var i = 0, max = closeBtns.length; i < max; i++) {
    var close = closeBtns[i];
    close.addEventListener('click', function() {
        modal.style.display = 'none';
    });
}

但是,為了不使用這么多全局變量,我建議稍微重構一下你的代碼。

暫無
暫無

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

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