下拉window.onclick javascript仅适用于一个菜单

时间:2018-10-23 22:26:53

标签: javascript html css drop-down-menu

如果只有一个,则此菜单有效。但是如果我想做两个, window.onclick = function(event)仅适用于一个菜单。为什么会这样并且容易修复?我如何才能使这两个菜单相同?..请只使用javascript ..没有jquery。

function openDrop() {
    document.getElementById("myDropdown").classList.toggle("show");
}

// Close the dropdown if the user clicks outside of it
window.onclick = function(event) {
  if (!event.target.matches('.dropbtn')) {

    var dropdowns = document.getElementsByClassName("dropdown-content");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
}
.dropbtn {
    background-color: #6dc14b;
    color: white;
    padding: 16px;
    font-size: 16px;
    border: none;
    cursor: pointer;
    width:100%;
}

.dropbtn:hover, .dropbtn:focus {
    background-color: #003400;
}

.dropdown {
    width:300px;
    margin:0 auto;
}

.dropdown-content {
    display: none;
    position: absolute;
    background-color: #ddd;
    min-width: 300px;
    overflow: auto;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    z-index: 1;
}

.dropdown-content a {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
}

.dropdown a:hover {background-color: #b9d8eb;}

.show {display: block;}

}
<div class="dropdown">
<button onclick="openDrop()" class="dropbtn">Company A</button>
  <div id="myDropdown" class="dropdown-content">
    <a>Type 1</a>
    <a href="#contact"> Type 2</a>
  </div>
</div>

1 个答案:

答案 0 :(得分:1)

您需要将下拉列表的id作为openDrop()函数的参数进行传递,这是一个快速解决方法:

function openDrop(menu) {
    document.getElementById(menu).classList.toggle("show");
}

// Close the dropdown if the user clicks outside of it
window.onclick = function(event) {
  if (!event.target.matches('.dropbtn')) {

    var dropdowns = document.getElementsByClassName("dropdown-content");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
}
.dropbtn {
    background-color: #6dc14b;
    color: white;
    padding: 16px;
    font-size: 16px;
    border: none;
    cursor: pointer;
    width:100%;
}

.dropbtn:hover, .dropbtn:focus {
    background-color: #003400;
}

.dropdown {
    width:300px;
}

.dropdown-content {
    display: none;
    position: absolute;
    background-color: #ddd;
    min-width: 300px;
    overflow: auto;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    z-index: 1;
}

.dropdown-content a {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
}

.dropdown a:hover {background-color: #b9d8eb;}

.show {display: block;}

}
<div class="dropdown">
<button onclick="openDrop('myDropdown1')" class="dropbtn">Company A</button>
  <div id="myDropdown1" class="dropdown-content">
    <a>Type 1</a>
    <a href="#contact"> Type 2</a>
  </div>
</div>

<div class="dropdown" style="float:right">
<button onclick="openDrop('myDropdown2')" class="dropbtn">Company B</button>
  <div id="myDropdown2" class="dropdown-content">
    <a>Type 3</a>
    <a href="#contact"> Type 4</a>
  </div>
</div>