【问题标题】:How to make only 1 popup open at a time?如何一次只打开一个弹出窗口?
【发布时间】:2016-09-10 16:11:40
【问题描述】:

以下面的代码为例,它目前允许同时打开两个弹出窗口。在我现实生活中的应用程序中,我将有 30 多个弹出窗口。 我希望将代码添加到脚本中,以便在打开新的弹出窗口时关闭之前打开的任何其他弹出窗口。我还希望在单击自身时仍然能够关闭弹出窗口(就像现在一样)。这可以用“if”语句来完成吗?请分享具体的例子。我不擅长创建自己的 JavaScript 代码,我只能复制/修改现有代码。 - 提前谢谢你!

这是我正在使用的示例:

<!DOCTYPE html>
<html>
<style>
/* Popup container - can be anything you want */
.popup {
    position: relative;
    display: inline-block;
    cursor: pointer;
}

/* The actual popup */
.popup .popuptext {
    visibility: hidden;
    width: 160px;
    background-color: #555;
    color: #fff;
    text-align: center;
    border-radius: 6px;
    padding: 8px 0;
    position: absolute;
    z-index: 1;
    bottom: 125%;
    left: 50%;
    margin-left: -80px;
}



/* Toggle this class - hide and show the popup */
.popup .show {
    visibility: visible;
}



</style>
<body style="text-align:center">

<h2>Popup</h2>

<div class="popup" onclick="myFunction1()">Click me to toggle the popup!
  <span class="popuptext" id="myPopup1">A Simple Popup!</span>
</div>
<br>
<br>
<br>
<div class="popup" onclick="myFunction2()">Click!
  <span class="popuptext" id="myPopup2">A Popup!</span>
</div>

<script>
// When the user clicks on div, open the popup
function myFunction1() {
    var popup = document.getElementById('myPopup1');
    popup.classList.toggle('show');
}

function myFunction2() {
    var popup = document.getElementById('myPopup2');
    popup.classList.toggle('show');
}


</script>

</body>
</html>

【问题讨论】:

    标签: javascript css popup


    【解决方案1】:

    在打开新元素之前,您只需要一种方法从所有 popuptext 元素中删除 show 类。您还可以通过使用一个函数并将参数传递给它以触发要触发的弹出窗口,从而消除对每个弹出元素使用单个函数的需要:

    // When the user clicks on div, open the popup
    function popup($target) {
        var popup = document.getElementById($target);
        closePopups($target);  
        popup.classList.toggle('show');
    }
    
    
    function closePopups($target) {
        var popups = document.getElementsByClassName('popuptext');
        for (i = 0; i < popups.length; i++) {
          if (popups[i].getAttribute('id') != $target) {
            popups[i].classList.remove('show');
            }
        }
    }
    /* Popup container - can be anything you want */
    .popup {
        position: relative;
        display: inline-block;
        cursor: pointer;
    }
    
    /* The actual popup */
    .popup .popuptext {
        visibility: hidden;
        width: 160px;
        background-color: #555;
        color: #fff;
        text-align: center;
        border-radius: 6px;
        padding: 8px 0;
        position: absolute;
        z-index: 1;
        bottom: 125%;
        left: 50%;
        margin-left: -80px;
    }
    
    
    
    /* Toggle this class - hide and show the popup */
    .popup .show {
        visibility: visible;
        -webkit-animation: fadeIn 1s;
        animation: fadeIn 1s;
    }
    <h2>Popup</h2>
    
    <div class="popup" onclick="popup('myPopup1')">Click me to toggle the popup!
      <span class="popuptext" id="myPopup1">A Simple Popup!</span>
    </div>
    <br>
    <br>
    <br>
    <div class="popup" onclick="popup('myPopup2')">Click!
      <span class="popuptext" id="myPopup2">A Popup!</span>
    </div>

    【讨论】:

    • 谢谢 - 这是向前迈出的一大步!但是,当我单击自身时,我仍然希望能够关闭弹出窗口。还有其他想法吗?
    • 糟糕,错过了那部分。编辑以解决该问题。干杯!
    • 你太棒了!非常感谢,我在这里的第一个问题,我不得不说 - 我印象非常深刻!
    • 如果没有 'jquery.min.js' 链接,这仍然有效吗?我将无法从我的网站获得外部链接! :(
    • 绝对。我在玩 sn-p 时无意中添加了 jQuery。我已经删除了它。没有必要完成你正在做的事情。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-12
    • 1970-01-01
    • 1970-01-01
    • 2021-06-30
    • 1970-01-01
    • 2010-11-23
    • 1970-01-01
    相关资源
    最近更新 更多