简体   繁体   English

无法使javascript下拉菜单正常工作

[英]Can't get javascript dropdown to work

I have a Javascript menu. 我有一个Javascript菜单。 The hover and click for the main menu items work however, I cannot get the dropdown to work. 悬停并单击主菜单项即可,但是我无法使用下拉菜单。

menu.js is menu.js是

 /* When the user clicks on the button,
     toggle between hiding and showing the dropdown content */
 function myFunction() {
   //document.getElementById("myDropdown").classList.toggle("show");
   //document.getElementsByClassName("dropdown-content").show);
   //document.getElementById("pmd").classList.toggle("show");
   pmd.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');
       }
     }
   }
   if(!event.target.matches('.dropbtnpm')) {
     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');
     }
   }
 }

My HTML for the menu where I want a dropdown is as follows. 我想要下拉菜单的HTML如下。 I'm just trying to get it to work with text of 'test'. 我只是想让它与“测试”文本一起使用。 I can do the link later 我以后可以做链接

 <td><button onmouseover="myFunction();"
 onclick="location.href='property-mngt.html';" value="Property Management"
 class="dropbtn">PROPERTY MANAGEMENT</button>
<div id="pmd" class="dropdown-content"> test </div>
 </td>

The CSS is CSS是

.dropbtn {
  border:  none;
  padding: 16px;
  cursor: pointer;
  background-color: #ffffff;
  color: black;
  height: 125px;
  min-height: 125px;
  font-size: 12px;
}
.dropbtn:hover, .dropbtn:focus {
  background-color: #004b8d;
  color: white;
  font-size: 12px;
}
.dropdown {
  position: relative;
  color: #ffff33;
}
.dropdown-content {
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  display: none;
}
.dropdown-content a {
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  color: white;
}
.dropdown a:hover {
  background-color: #f1f1f1;
  color: black;
}
.show {
  border: 1px solid #33ccff;
  display: block;
  visibility: visible;
  background-color: #004b8d;
  color: white;
  font-family: Arial,Helvetica,sans-serif;
  font-size: small;
}
.dropdown-content a:hover {
  background-color: #f1f1f1;
  color: black;
}

My goal is to have the dropdown list appear when hovering over that particular menu item. 我的目标是将鼠标悬停在特定菜单项上时出现下拉列表。 If I change the html to <div id="pmd" class="dropdown-content.show"> test </div> 'test' shows on the menu. 如果我将html更改为<div id="pmd" class="dropdown-content.show"> test </div> “ test”显示在菜单上。

I'm stuck on getting the mouseover of that menu item to show the dropdown. 我停留在将鼠标悬停在该菜单项上来显示下拉菜单。

you're trying to show multiple elements. 您正在尝试显示多个元素。 take the first and and display it correctly. 首先,并正确显示它。

document.getElementsByClassName('dropdown-content')[0].style.display = 'block'

or use use the document.querySelector('dropdown-content') 或使用document.querySelector('dropdown-content')

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

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