HTML的新手。我只是想弄清楚如何为一个小项目创建下拉按钮。这是我在w3上找到的下拉按钮的代码。

我想有多个下拉按钮。如果我复制<div class="dropdown">,则单击任一按钮只会触发第一个下拉按钮。

到底是什么触发了这一点?创建多个按钮的正确方法是什么?



    /* 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');
          }
        }
      }
    }

    .dropbtn {
        background-color: #4CAF50;
        color: white;
        padding: 16px;
        font-size: 16px;
        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: 160px;
        overflow: auto;
        box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    }

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

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

    .show {display:block;}
    </style>
    </head>

    <h2>Clickable Dropdown</h2>
    <p>Click on the button to open the dropdown menu.</p>

    <div class="dropdown">
    <button onclick="myFunction()" class="dropbtn">Dropdown</button>
      <div id="myDropdown" class="dropdown-content">
        <a href="#home">Home</a>
        <a href="#about">About</a>
        <a href="#contact">Contact</a>
      </div>
    </div>

最佳答案

我已经修改了您的代码,这是您所需要的吗?

<!DOCTYPE html>
<html>
  <head>
    <style>
      .dropbtn {
        background-color: #4CAF50;
        color: white;
        padding: 16px;
        font-size: 16px;
        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: 160px;
      overflow: auto;
      box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  }

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

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

.show {display:block;}
</style>
</head>
<body>

  <h2>Clickable Dropdown</h2>
  <p>Click on the button to open the dropdown menu.</p>

  <div class="dropdown">
  <button onclick="myFunction('myDropdown1')" class="dropbtn">Dropdown</button>
  <div id="myDropdown1" class="dropdown-content">
    <a href="#home">Home</a>
    <a href="#about">About</a>
    <a href="#contact">Contact</a>
  </div>
</div>
<div class="dropdown">
  <button onclick="myFunction('myDropdown2')" class="dropbtn">Dropdown 2</button>
  <div id="myDropdown2" class="dropdown-content">
    <a href="#home">Home 2</a>
    <a href="#about">About 2</a>
    <a href="#contact">Contact 2</a>
  </div>
</div>

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

  var dropdowns = document.getElementsByClassName("dropdown-content");
  var i;

  for (i = 0; i < dropdowns.length; i++) {
    var openDropdown = dropdowns[i];

    if (openDropdown.id !== id && openDropdown.classList.contains('show')) {
      openDropdown.classList.remove('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');
     }
    }
  }
}
</script>

</body>
</html>

关于html - HTML-帮助我了解此脚本的工作原理,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40145869/

10-12 12:24
查看更多