【问题标题】:Have Multiple Click-to-Open Dropdown Menus Only Want One Open at a Time有多个点击打开下拉菜单一次只希望打开一个
【发布时间】:2017-01-26 18:51:50
【问题描述】:

首先,我是 javascript、html 和 CSS 的新手,所以请耐心等待。我到处寻找问题的答案,但找不到任何适用于我的特定代码的东西。

我正在尝试创建一个具有多个下拉菜单的网页,并且每个下拉菜单都会在用户单击它时打开。我能够做到这一点,但出现了问题。如果我打开一个下拉菜单,然后单击另一个下拉菜单,则第一个菜单保持打开状态。我希望在打开新菜单时关闭第一个菜单。

这是我的 html 代码的一部分,其中包含 2 个下拉菜单:

<table class="prodMenu">
<tr><td>
<div class="dropdown2">
<button onclick="myFunction('m1')" class="dropbtn2">SPCGuidance</button>
    <div id="m1" class="dropdown2-content">
        <a href="sseonew12.php?run=<?php print $inrun ?>&cycle=<?php print $incyc ?>&sector=<?php print $insect ?>&id=4PR-TORN">[PR]:4-hr Calibrated Tornado Probability</a>
        <a href="sseonew12.php?run=<?php print $inrun ?>&cycle=<?php print $incyc ?>&sector=<?php print $insect ?>&id=4PR-HAIL">[PR]:4-hr Calibrated Hail Probability</a>
    </div>
</div>
</td>
<td>
<div class="dropdown2">
<button onclick="myFunction('m2')" class="dropbtn2">Reflectivity</button>
    <div id="m2" class="dropdown2-content">
        <a href="sseonew12.php?run=<?php print $inrun ?>&cycle=<?php print $incyc ?>&sector=<?php print $insect ?>&id=3SP-1KM-REFL40">[SP]:3-hr 1-km Reflectivity >=40 dBZ</a>
        <a href="sseonew12.php?run=<?php print $inrun ?>&cycle=<?php print $incyc ?>&sector=<?php print $insect ?>&id=3NPR-1KM-REFL40">[NPRS]:3-hr 1-km Reflectivity >=40 dBZ</a>
    </div>
</div>
</td>

接下来是与这些下拉菜单交互的 .js 脚本部分。如果您单击窗口中的某个位置,我确实有一个关闭打开菜单的功能。但是,我不确定如何创建一个在打开另一个下拉菜单时关闭第一个下拉菜单的功能。

// When the user clicks on the button, toggle between hiding and showing the dropdown content.
function myFunction(id) {
        document.getElementById(id).classList.toggle("show");
}

// Close the dropdown if the user clicks in window.
window.onclick = function(event) {
        if (!event.target.matches('.dropbtn2')) {
                var dropdowns = document.getElementsByClassName("dropdown2-content");
                var i;
                for (i = 0; i < dropdowns.length; i++) {
                        var openDropdown = dropdowns[i];
                        if (openDropdown.classList.contains('show')) {
                                openDropdown.classList.remove('show');
                        }
                }
        }
}

最后是控制下拉菜单的 CSS 脚本部分:

/* dropdown2 is for the rest of the dropdown menus. */
.dropbtn2 {
    background-color: #444444;
    color: #FFFFFF;
    margin: 0 1px 0 0;
    padding: 4px 3px;
    width: auto;
    font: bold 10px arial;
    cursor: pointer;
    text-align: center;
    white-space: nowrap;
    text-decoration: none;
    border: none;
}
.dropbtn2:hover, .dropbtn2:focus {
    background-color: #000066;
    border: none;
}
.dropdown2 {
    position: relative;
    display: inline-block;
    z-index: 30;
.dropdown2-content {
    display: none;
    position: absolute;
    padding: 0px;
    width: auto;
    min-width: 160px;
    white-space: nowrap;
    background: #DDDDDD;
    overflow: auto;
    z-index: 1;
    border-style: solid;
    border-width: 1px;
    border-color: #000000;
}
.dropdown2-content a {
    color: #000000;
    padding: 2px 3px;
    font: 10px arial;
    display: block;
}
.dropdown2 a:hover {
    background: #000066;
    color: #FFF;
    border: none;
    text-decoration: none;
}
.show {
    display:block;
}

任何帮助将不胜感激。谢谢。

编辑:

我明白了。

对于 Javascript 部分,当您单击另一个下拉菜单、在窗口外部单击或再次单击同一菜单的标题时,这会成功关闭当前下拉菜单。

function myFunction(id) {
        var dropdowns = document.getElementsByCLassName("dropdown2-content");
                var i;
                for (i = 0; i < dropdowns.length; i++) {
                        var openDropdown = dropdowns[i];
                        if ( dropdowns[i] != document.getElementById(id) ) {
                                openDropdown.classList.remove('show');
                        }
                }
          document.getElementById(id).classList.toggle("show");
}
// Close the dropdown if the user clicks in window.
window.onclick = function(event) {
        if (!event.target.matches('.dropbtn2')) {
                var dropdowns = document.getElementsByClassName("dropdown2-  content");
                var i;
                for (i = 0; i < dropdowns.length; i++) {
                        var openDropdown = dropdowns[i];
                        if (openDropdown.classList.contains('show')) {
                                openDropdown.classList.remove('show');
                        }
                }
        }
}

【问题讨论】:

  • 可以用jQuery吗?
  • 我以前从未使用过jQuery,但看过sn-ps的代码。这可能是可能的,但我不确定如何实施。

标签: javascript jquery html css drop-down-menu


【解决方案1】:

您可以在打开被点击的下拉菜单之前关闭所有下拉菜单

function myFunction(id) {
    var dropdowns = document.getElementsByClassName("dropdown2-content");
            var i;
            for (i = 0; i < dropdowns.length; i++) {
                    var openDropdown = dropdowns[i];
                            openDropdown.classList.remove('show');
            }
    document.getElementById(id).classList.toggle("show");
}

【讨论】:

  • 谢谢。这适用于在打开另一个菜单时关闭上一个菜单。但是,现在如果我打开了一个菜单并想要关闭它,点击它的标题就不再起作用了。我必须单击不同的菜单或窗口中的某个位置才能关闭它。您对此有什么建议吗?
【解决方案2】:

如果您可以包含 jQuery 并使用它,这将起作用:

$(document).ready(function(){
    $(document).on('click','.dropbtn2',function(){
        $('.dropbtn2').not(this).next().removeClass('show');
        $(this).next().toggleClass('show');
    });
    $(document).on('click',function(e){
        if(!$(e.target).closest('.dropbtn2').length)
            $('.dropbtn2').next().removeClass('show');
    });    
});
/* dropdown2 is for the rest of the dropdown menus. */
.dropbtn2 {
    background-color: #444444;
    color: #FFFFFF;
    margin: 0 1px 0 0;
    padding: 4px 3px;
    width: auto;
    font: bold 10px arial;
    cursor: pointer;
    text-align: center;
    white-space: nowrap;
    text-decoration: none;
    border: none;
}
.dropbtn2:hover, .dropbtn2:focus {
    background-color: #000066;
    border: none;
}
.dropdown2 {
    position: relative;
    display: inline-block;
    z-index: 30;
}
.dropdown2-content {
    display: none;
    position: absolute;
    padding: 0px;
    width: auto;
    min-width: 160px;
    white-space: nowrap;
    background: #DDDDDD;
    overflow: auto;
    z-index: 1;
    border-style: solid;
    border-width: 1px;
    border-color: #000000;
}
.dropdown2-content a {
    color: #000000;
    padding: 2px 3px;
    font: 10px arial;
    display: block;
}
.dropdown2 a:hover {
    background: #000066;
    color: #FFF;
    border: none;
    text-decoration: none;
}
.show {
    display:block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<table class="prodMenu">
<tr><td>
<div class="dropdown2">
<button class="dropbtn2">SPCGuidance</button>
    <div id="m1" class="dropdown2-content">
        <a href="#">[PR]:4-hr Calibrated Tornado Probability</a>
        <a href="#">[PR]:4-hr Calibrated Hail Probability</a>
    </div>
</div>
</td>
<td>
<div class="dropdown2">
<button class="dropbtn2">Reflectivity</button>
    <div id="m2" class="dropdown2-content">
        <a href="#">[SP]:3-hr 1-km Reflectivity >=40 dBZ</a>
        <a href="#">[NPRS]:3-hr 1-km Reflectivity >=40 dBZ</a>
    </div>
</div>
</td>

【讨论】:

    【解决方案3】:

    我想说最好的解决方案是使用 Bootstrap,它可以开箱即用地处理这些情况:看看它是如何工作的 http://getbootstrap.com/components/#btn-dropdowns

    如果你因为某种原因不能使用 Bootstrap 而可以使用 jQuery,那也很容易。单击按钮时,您将隐藏所有下拉菜单,然后仅显示相邻的下拉菜单。它会是这样的:

    $('.dropbtn2').click(function(){
      $('.dropdown2-content).hide();  // hide all the dropdowns
      $(this).next().show();          // show the next sibling
    });
    

    如果您既不能使用 Bootstrap 也不能使用 jQuery,只需复制 windows.onclick 部分中的代码,然后再将元素显示在 myFunction 中,正如 Funk Doc 所说。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-26
      • 2016-09-17
      • 2021-10-19
      • 2017-01-10
      • 1970-01-01
      • 1970-01-01
      • 2020-04-30
      • 1970-01-01
      相关资源
      最近更新 更多