链接不仅适用于我的下拉按钮的一个选项

时间:2017-06-17 15:28:59

标签: button drop-down-menu dropdown

我制作了这个html和jscript下拉按钮但是当我尝试添加更多按钮时,下拉菜单中的链接仅适用于一个按钮,并且在与第一个按钮相同的链接中更改另外两个按钮。

例如,如果第一个按钮在下拉列表'#ro'的内容中有一个链接,而另外两个按钮有'#en'和'#de',它将为所有人显示'#ro'。

如何解决此问题并为所有按钮创建单独的链接? 这是我的代码

/* When the user clicks on the button, 
toggle between hiding and showing the dropdown content */
function myFunction() {
    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');
      }
    }
  }
}
.hide {
    display:none;
 }
.visible {
    display:block;
 }

.dropbtn {
    background-color: #602121;
    color: white;
    padding: 2px;
    font-size: 10px;
    border: none;
    cursor: pointer;
}

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

.dropdown {
    position: relative;
    display: inline-block;
}

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

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

.dropdown a:hover {background-color: #f1f1f1}

.show {display:block;}
<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">RO</button>
  <div id="myDropdown" class="dropdown-content">
    <center><a href="#ro">Salveaza</a></center>
  </div>
</div>

<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">EN</button>
  <div id="myDropdown" class="dropdown-content">
    <a href="#en">Save</a>
  </div>
</div>

<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">DE</button>
  <div id="myDropdown" class="dropdown-content">
    <a href="#de">Save</a>
  </div>
</div>

0 个答案:

没有答案