HTML - 帮助我理解这个脚本的工作原理

时间:2016-10-20 05:07:28

标签: html css

这里的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>

2 个答案:

答案 0 :(得分:0)

我修改了你的代码,这是你需要的吗?

<!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>

答案 1 :(得分:0)

回答你的问题

  

如果我复制,点击任一按钮只会触发第一个下拉按钮。具体是触发这个

我猜你是在复制整个div,就像这样?

<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>

这将有效重复您的ID ,因此myFunction()的代码片段如下所示

 document.getElementById("myDropdown")

选择它遇到的第一个 #myDropdown并执行切换类的魔力,因此您觉得它只会触发第一次下拉。

  

创建多个按钮的正确方法是什么?

实际上有很多方法,但是因为你是一个初学者,我认为我可以放心地说复制粘贴很好:)只要你不重复这些ID。

但如果你真的在寻找代码的修复方法,我想@Lahiru的答案应该涵盖你!

干杯和快乐的学习