简体   繁体   English

下拉菜单-如何将多个按钮链接到javascript功能

[英]Drop Down Menu- How to link multiple buttons to javascript function

I am trying to develop a drop down menu. 我正在尝试开发一个下拉菜单。 I have written html code and javascript for two buttons, but I am wondering how to link the second button to javascript. 我已经为两个按钮编写了html代码和javascript,但是我想知道如何将第二个按钮链接到javascript。 Do I need to write another function() ? 我需要编写另一个function()吗?

<div class="dropmenu">
  <button onclick="myFunction()" class="button1" style="background-color:#61117F ;">Dropdown</button>
  <div id="Dropdown1" class="dropmenu-content">
    <a href=" http://www.apple.com/">Apple</a>
    <a href="https://www.google.com/">google</a>
  </div>
</div>

<div class="dropmenu">
  <button onclick="myFunction()" class="button1" style="background-color:#d7791b ;">DropDown2</button>
  <div id="Dropdown2" class="dropmenu-content">
    <a href=" https://www.yahoo.com/">Yahoo</a>
    <a href="https://www.facebook.com/">FB</a>
  </div>
</div>

<script>
  function myFunction() {
      document.getElementById("Dropdown1").classList.toggle("show");
      window.onclick = function(event) {
          if (!event.target.matches('.button1')) {
            var drop = document.getElementsByClassName("dropmenu-content");
            var i;
            for (i = 0; i < drop.length; i++) {
              var Dropdown = drop[i];
              if (Dropdown.classList.contains('show')) {
                Dropdown.classList.remove('show');
              }
            }}}
</script>

Your JavaScript can be simplified a lot. 您的JavaScript可以大大简化。 Just pass the clicked button to the function and then find it's sibling with the class dropmenu-content . 只需将单击的按钮传递给该函数,然后找到它与class dropmenu-content的同级按钮即可。 After you have that, all you need to do is toggle the show class. 完成后,您所需要做的就是切换show类。

 window.myFunction = function(e) { var dropdown = e.parentNode.getElementsByClassName('dropmenu-content')[0]; dropdown.classList.toggle('show'); } 
 .dropmenu-content { display: none; } .dropmenu-content.show { display: block; } 
 <div class="dropmenu"> <button onclick="myFunction(this);" class="button1" style="background-color:#61117F ;">Dropdown</button> <div id="Dropdown1" class="dropmenu-content"> <a href=" http://www.apple.com/">Apple</a> <a href="https://www.google.com/">google</a> </div> </div> <div class="dropmenu"> <button onclick="myFunction(this);" class="button1" style="background-color:#d7791b ;">DropDown2</button> <div id="Dropdown2" class="dropmenu-content"> <a href=" https://www.yahoo.com/">Yahoo</a> <a href="https://www.facebook.com/">FB</a> </div> </div> 

You could give your button a data-attribute with the id of the dropdown you want to affect and a css class of something like "trigger-event" to fire the event 您可以为按钮提供一个数据属性,其中包含您要影响的下拉列表的ID,并使用css类(例如“ trigger-event”)触发事件

<button data-dropdown="Dropdown1" class="trigger-event" style="...">
...
<button data-dropdown="Dropdown2" class="trigger-event" style="...">

and use the data attribute to retrieve the dropdown 并使用data属性检索下拉列表

$(document).on('click', '.trigger-event', function(){
    var dropdownId = $(this).data("dropdown");//or attr("data-dropdown")
    myFunction(dropdownId);
});

function myFunction(dropdownId) {
    document.getElementById(dropdownId).classList.toggle("show");
    window.onclick = function(event) {
    ...
    }
}

 function myFunction(dr){ var vis = document.getElementById(dr).style.display == "block" ? "none" : "block"; document.getElementById(dr).style.display = vis; } 
 .button1{ border:none; border-radius:5px; padding:10px; color:white; margin:5px; } .dropmenu-content{ margin:10px; } .dropmenu-content a{ text-decoration:none; color:brown; box-shadow:1px 1px #ccc; padding:5px; border-left:solid 3px green; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="dropmenu"> <button onclick="myFunction('Dropdown1')" class="button1" style="background-color:#61117F ;">Dropdown1 &#x25BC;</button> <div id="Dropdown1" style='display:none;' class="dropmenu-content"> <a href=" http://www.apple.com/">Apple</a> <a href="https://www.google.com/">google</a> </div> </div> <div class="dropmenu"> <button onclick="myFunction('Dropdown2')" class="button1" style="background-color:#d7791b ;">DropDown2 &#x25BC;</button> <div id="Dropdown2" style='display:none;' class="dropmenu-content"> <a href=" https://www.yahoo.com/">Yahoo</a> <a href="https://www.facebook.com/">FB</a> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM