单击时如何使包含下拉列表的导航栏变暗?

时间:2018-08-22 08:09:28

标签: jquery html drop-down-menu navbar

我已经学习了如何通过以下代码使导航栏上所选选项的背景变暗:

 <script type="text/javascript">
    $(function(){
        $('.nav a').filter(function(){
            return this.href==location.href}).parent().addClass('active').siblings().removeClass('active')
        $('.nav a').click(function(){
            $(this).parent().addClass('active').siblings().removeClass('active')    
        })
    })
</script>

这是它的工作方式: darker!

这是我的导航栏代码:

<li ><a href="index.php">About</a></li>
    <li action="active"><a href="news.php">News</a></li>
    <li><a href="intern.php">Job</a></li>
    <li class=dropdown>
        <a class=dropdown-toggle data-toggle=dropdown href=#>Student <span class=caret></span></a>
        <ul class=dropdown-menu>
            <li><a href='resume.php'>Resume</a></li>
            <li><a href='history.php'>History</a></li>
            <li><a href='upload.php'>Upload</a></li>                        
       </ul>
    </li>
    <li><a href="download.php">Download</a></li>

但是在这里我遇到了另一个问题:
有些选项包含一个下拉列表,例如 student ,如果我单击它,则深色背景会消失,但应该会使学生背景变暗。

The navbar

1 个答案:

答案 0 :(得分:0)

尝试此代码可能会对您有所帮助

<!DOCTYPE html>
    <html>
    <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <style>
    body {margin:0;font-family:Arial}

    .topnav {
      overflow: hidden;
      background-color: #333;
    }

    .topnav a {
      float: left;
      display: block;
      color: #f2f2f2;
      text-align: center;
      padding: 14px 16px;
      text-decoration: none;
      font-size: 17px;
    }

    .active {
      background-color: #4CAF50;
      color: white;
    }

    .topnav .icon {
      display: none;
    }

    .dropdown {
        float: left;
        overflow: hidden;
    }

    .dropdown .dropbtn {
        font-size: 17px;    
        border: none;
        outline: none;
        color: white;
        padding: 14px 16px;
        background-color: inherit;
        font-family: inherit;
        margin: 0;
    }

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

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

    .topnav a:hover, .dropdown:hover .dropbtn {
      background-color: #555;
      color: white;
    }

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

    .dropdown:hover .dropdown-content {
        display: block;
    }

    @media screen and (max-width: 600px) {
      .topnav a:not(:first-child), .dropdown .dropbtn {
        display: none;
      }
      .topnav a.icon {
        float: right;
        display: block;
      }
    }

    @media screen and (max-width: 600px) {
      .topnav.responsive {position: relative;}
      .topnav.responsive .icon {
        position: absolute;
        right: 0;
        top: 0;
      }
      .topnav.responsive a {
        float: none;
        display: block;
        text-align: left;
      }
      .topnav.responsive .dropdown {float: none;}
      .topnav.responsive .dropdown-content {position: relative;}
      .topnav.responsive .dropdown .dropbtn {
        display: block;
        width: 100%;
        text-align: left;
      }
    }
    </style>
    </head>
    <body>

    <div class="topnav" id="myTopnav">
      <a href="#home" class="active">Home</a>
      <a href="#news">News</a>
      <a href="#contact">Contact</a>
      <div class="dropdown">
        <button class="dropbtn">Dropdown 
          <i class="fa fa-caret-down"></i>
        </button>
        <div class="dropdown-content">
          <a href="#">Link 1</a>
          <a href="#">Link 2</a>
          <a href="#">Link 3</a>
        </div>
      </div> 
      <a href="#about">About</a>
      <a href="javascript:void(0);" style="font-size:15px;" class="icon" onclick="myFunction()">&#9776;</a>
    </div>

    <div style="padding-left:16px">
      <h2>Responsive Topnav with Dropdown</h2>
      <p>Resize the browser window to see how it works.</p>
      <p>Hover over the dropdown button to open the dropdown menu.</p>
    </div>

    <script>
    function myFunction() {
        var x = document.getElementById("myTopnav");
        if (x.className === "topnav") {
            x.className += " responsive";
        } else {
            x.className = "topnav";
        }
    }
    </script>

    </body>
    </html>