簡體   English   中英

如何按順序關閉最后打開的彈出窗口?

[英]How can I close the last opened popup in order?

如何按順序關閉最后打開的彈出窗口?

下圖是每當打開彈出窗口時,彈出標簽都會向下堆疊的圖像。
在此處輸入圖像描述

下面是應用源。

/* ESC popup close start */
$(document).on('keyup', function(e) {
  if (e.key == "Escape") $('.window .close').last().click();
});
/* ESC popup close end */
  • 確保點擊觸發前的close點擊事件
  • 您需要將addClass添加到最后一個.window元素,以避免在您再次訂購最后一個項目時使用它

 $(".close").on("click", function(){ $(this).closest(".window").hide(); }); /* ESC popup close start */ $(document).on('keyup', function(e) { if (e.key == "Escape") $('.window:not(.hidden)').last().addClass("hidden").find(".close").click(); }); /* ESC popup close end */
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="window">Window 1<div class="close">X</div></div> <div class="window">Window 2<div class="close">X</div></div> <div class="window">Window 3<div class="close">X</div></div>

只需將彈出窗口 window 設置為任何變量,然后使用window.close使用該變量關閉

const daddyWindow = window.open("", "window" + number, "width=200,height=100");
daddyWindow.document.write('window Popup code');
daddyWindow.close();

嘗試這個:

<html>
<header>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script>
        let openedWindows = [];
        let newWindow;
        let number = 0;
        function openWindow() {
            number++;
            newWindow = window.open("", "window" + number, "width=200,height=100");
            newWindow.document.write(`<a href="javascript:window.close();"class="video-close">Close this window ${number}</a>`);
            openedWindows.push(newWindow);
        }

        $(document).on('keyup', function (e) {
            if (e.key == "Escape") {
                if (openedWindows && openedWindows.length >= 0 && openedWindows[openedWindows.length - 1]) {
                    openedWindows[openedWindows.length - 1].close();
                    openedWindows.pop()
                }
            }
        });
    </script>
</header>

<body>
    <h3 style="color:brown"> Close Window Example </h3>
    <button onclick="openWindow()">Open New Window</button>
</body>

</html>

暫無
暫無

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

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