下拉菜单在导航栏内不起作用,但在外面工作

时间:2018-04-27 16:33:56

标签: javascript html css

我正在尝试将下拉菜单添加到导航栏。但是当我点击下拉菜单时它没有显示菜单。它的菜单没有下拉。我寻找解决方案还有其他相关问题。但那些特定于代码的问题与我的代码无关。

如果我测试下拉代码,将其带到navgar之外就可以了。但在导航栏内却没有。谁能帮帮我吗?如果您需要有关阅读整体的帮助或需要任何解释,请询问。我需要帮助。谢谢。

所以这是我的HTML:

<nav class="navbar navbar-inverse navbar-fixed-top">
    <div class="container-fluid">
      <div class="navbar-header">
        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>                        
        </button>
      </div>
      <div class="collapse navbar-collapse" id="myNavbar">
        <ul class="nav navbar-nav">
          <li class="active"><a href="/"><span class="glyphicon glyphicon-home"></span> Home</a></li>
//This is the part of dropdown//
            <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>
          </div> 
        </div>
        </ul>
      </div>
    </div>
</nav>

这是我申请的CSS:

<style>
  .dropbtn {
      background-color: #3498DB;
      color: white;
      padding: 16px;
      font-size: 16px;
      border: none;
      cursor: pointer;
  }

  .dropbtn:hover, .dropbtn:focus {
      background-color: #2980B9;
  }

  .dropdown {
      position: relative;
      display: inline-block;
  }

  .dropdown-content {
      display: none;
      position: absolute;
      background-color: #f1f1f1;
      min-width: 160px;
      overflow: auto;
      box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
      z-index: 1;
  }

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

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

  .show {display:block;}

  body{
    background-color:#f1f1f1;
  }

      /* Remove the navbar's default margin-bottom and rounded borders */ 
      .navbar {
        overflow: hidden;
        margin-bottom: 0;
        border-radius: 0;
        width: 100% ;
        height: 30px;
      }
      .navbarw3 {
      overflow: hidden;
      background-color: #333;
      font-family: Arial, Helvetica, sans-serif;
  }

  .navbar a {
      float: left;
      font-size: 16px;
      color: white;
      text-align: center;
      padding: 14px 16px;
      text-decoration: none;
  }
   .main {
      margin-top: 40px;  Add a top margin to avoid content overlay 
      }

      /* Set height of the grid so .sidenav can be 100% (adjust as needed) */
      .row.content {height: 565px}

      /* Set gray background color and 100% height */
      .sidenav {
        padding-top: 20px;
        background-color: #f1f1f1;
        height: 100%;
      }

      /* Set black background color, white text and some padding */
      footer {
        background-color: #555;
        color: white;
        padding: 15px;
      }

      /* On small screens, set height to 'auto' for sidenav and grid */
      @media screen and (max-width: 767px) {
        .sidenav {
          height: auto;
          padding: 15px;
        }
        .row.content {height:auto;} 
      }
    </style>

这是javascript:

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

1 个答案:

答案 0 :(得分:0)

根据您在HTML中使用的css类,我确定您使用的是bootstrap。在这种情况下,您真的不需要添加任何javascript来使用下拉菜单。确保你在页面中链接了bootstrap css和js文件,然后只需将html更改为:

<nav class="navbar justify-content-center navbar-expand-lg blackBg">
<ul class="nav">
    <li class="nav-item">
        <a class="nav-link" href="home.html">Home</a>
    </li>
    <li class="nav-item">
        <a class="nav-link" href="about.html">About</a>
    </li>
    <li class="nav-item">
        <a class="nav-link" href="blog.html">Blog</a>
    </li>
    <li class="nav-item">
        <a class="nav-link" href="contact.html">Contact</a>
    </li>
    <li class="dropdown nav-item">
        <a class="dropdown-toggle nav-link" data-toggle="dropdown" href="#">Gallery
            <span class="caret"></span>
        </a>
        <ul class="dropdown-menu blackBg">
            <li>
                <a href="#">Smiles</a>
            </li>
            <li>
                <a href="#">Cry's</a>
            </li>
            <li>
                <a href="#">Videos</a>
            </li>
            <li>
                <a href="#">Choreography</a>
            </li>
            <li>
                <a href="#"></a>
            </li>
        </ul>
    </li>
</ul>

这是一个例子:https://jsfiddle.net/dmkh8xj1/