简体   繁体   English

试图用JavaScript制作下拉菜单

[英]trying to make drop down menus with javascript

I am trying to make a nav bar with two drop down menus "appliances" and "furniture" i can get one to work but i am having trouble with the second one, i am assuming my function is only applying to one of them at a time but cannot figure out why the other is not working nor can i get it to work. 我正在尝试使用两个下拉菜单“设备”和“家具”制作一个导航栏,我可以使其中一个工作,但第二个菜单却有麻烦,我假设我的功能仅适用于一个菜单。时间,但无法弄清楚为什么另一个无法正常工作,我也无法使其正常工作。 ` `

<table>
  <tr>
     <td><a href="home.html">Home</a>
     <td class="dropdown"><a href="javascript:void(0)" class="dropbtn" onclick="myFunction()">Appliances</a>
    <div class="dropdown-content" id="drop">
      <a href="#">Link 1</a>
      <a href="#">Link 2</a>
     <td><td class="dropdown"><a href="javascript:void(0)" class="dropbtn" onclick="myFunction()">Furniture</a>
    <div class="dropdown-content" id="drop">
      <a href="#">Link 1</a>
      <a href="#">Link 2</a>
     <td><a href="bed.html">Bedding</a>
     <td><a href="contact.html">Contact Us</a>
     <td><a href="cart.html">Shopping Cart</a>
  </tr>
</table>


<script>

function myFunction() {
    document.getElementById("drop").classList.toggle("show");   
}

window.onclick = function(e) {
  if (!e.target.matches('.dropbtn')) {

    var dropdowns = document.getElementsByClassName("dropdown-content");
    for (var d = 0; d < dropdowns.length; d++) {
      var openDropdown = dropdowns[d];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
}
</script>

here is the css 这是CSS

    td a, .dropbtn {
    display: inline-block;
    color: #b35900;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
}

td a:hover, .dropdown:hover .dropbtn {
    background-color:#ff0000;
}

td.dropdown {
    display: inline-block;
}

.dropdown-content {
    display: none;
    position: absolute;
    background-color: #ff000;
    min-width: 160px;
    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;
    text-align: left;
}

.dropdown-content a:hover {background-color: #ff0000}
<table>
  <tr>
   <td><a href="home.html">Home</a>
   <td class="dropdown"><a href="javascript:void(0)" class="dropbtn" onclick="myFunction("drop1")">Appliances</a>
<div class="dropdown-content" id="drop1">
  <a href="#">Link 1</a>
  <a href="#">Link 2</a>
 <td><td class="dropdown"><a href="javascript:void(0)" class="dropbtn" onclick="myFunction("drop2")">Furniture</a>
<div class="dropdown-content" id="drop2">
  <a href="#">Link 1</a>
  <a href="#">Link 2</a>
     <td><a href="bed.html">Bedding</a>
     <td><a href="contact.html">Contact Us</a>
     <td><a href="cart.html">Shopping Cart</a>
  </tr>
</table>


<script>

    function myFunction(id) {
        document.getElementById(id).classList.toggle("show");   
    }

</script>

At least its a beginning. 至少是一个开始。 You need unique ids on everything in your dom, so the getElemenetById call will only find one of them. 您在dom中的所有内容上都需要唯一的ID,因此getElemenetById调用只能找到其中之一。 Adding the id as a param will seperate them. 将id添加为参数将分隔它们。

For something like a menu, using id is probably more hassle than its worth (down the line anyway) 对于诸如菜单之类的东西,使用id可能比其价值更麻烦(无论如何都行)

When an event is fired an event variable is passed to the handler, it's got everything you need to be able to identify the pressed element 触发事件时,会将事件变量传递给处理程序,它具有识别被按下元素所需的一切

function myFunction(e) {
    e=e||event;
    if (e.target.classList.contains("dropdown")) {
      e.target.classList.toggle("show");
    }
}

I haven't tested this, and I dont think it will work with the code you've presented as there is no css definition for "show" unless you missed it when pasting it. 我尚未对此进行测试,并且我认为它不会与您提供的代码一起使用,因为“显示”没有CSS定义,除非您在粘贴时错过了它。

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

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