单击图标后如何显示我的列表项?

我为移动用户编写了一个下拉菜单,但它不起作用。该网站是响应式的,因此您可以看到宽度为 700 像素的下拉菜单图标。单击该图标后,应打开菜单,并且列表项应显示在彼此顶部的列表中。


我使用了 JavaScript:


$(document).ready(function() {

  $(".fa-bars").on("click", function() {

    $("header nav ul li").toggleClass("open");

  });

});

HTML:


    <!DOCTYPE html>

<html dir="ltr">

  <head>

    <head>

      <meta charset="utf-8">

      <title>Daniel | Website</title>


      <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@500&display=swap" rel="stylesheet">

      <link href="https://fonts.googleapis.com/css2?family=Roboto+Slab&display=swap" rel="stylesheet">


      <script src="https://code.jquery.com/jquery-1.12.4.min.js" integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ=" crossorigin="anonymous"></script>

      <script src="../../js/mobile-menu.js"></script>#

      <script src="js/lernen.js"></script>


      <link rel="stylesheet" type="text/css" href="../../css/all.css">

      <link rel="stylesheet" type="text/css" href="style.css">



    </head>

  </head>

  <body>


<!-- HEADER ---------------------------------------------------------------------------------------------->


    <header>

      <div id="logo">

        <a href="../home/"><img src="../../img/logo.png" alt="Das Logo wurde nicht gefunden!!"></a>

      </div>


      <nav id="main-nav">

        <i class="fa fa-bars" aria-hidden="true"></i>

        <ul>

          <li><a href="#home"> Lernen </a></li>

      <li><a href="#was"> Was? </a></li>

      <li><a href="#fuer-wen"> Für wen? </a></li>

      <li><a href="#kontakt"> Kontakt </a></li>

      <li><i class="fas fa-users-cog", id="user-cog"></i></li>


        </ul>


      </nav>


    </header>

CSS:


header nav ul li{

    display: none;

  }


header nav ul li.open{

    display: block;

    float: none;

    padding-top: 10px;

    padding-bottom: 10px;

  }


人到中年有点甜
浏览 142回答 1
1回答

慕虎7371278

当我将您自己的代码复制并粘贴到此处的编辑器中时,它会起作用。有一些关于我必须清理的标记的事情(<head>标签已经重复,需要关闭body和标签),但这让我认为问题可能不是 Javascript 或 CSS。html作为一个不相关的旁白,您可能会考虑显示和隐藏ul而不是所有的lis。效果相同,但选择和切换的内容更少。$(document).ready(function() {&nbsp; $(".fa-bars").on("click", function() {&nbsp; &nbsp; $("header nav ul li").toggleClass("open");&nbsp; });});header nav ul li{&nbsp; &nbsp; display: none;&nbsp; }header nav ul li.open{&nbsp; &nbsp; display: block;&nbsp; &nbsp; float: none;&nbsp; &nbsp; padding-top: 10px;&nbsp; &nbsp; padding-bottom: 10px;&nbsp; }<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>&nbsp; &nbsp; <header>&nbsp; &nbsp; &nbsp; <div id="logo">&nbsp; &nbsp; &nbsp; &nbsp; <a href="../home/"><img src="../../img/logo.png" alt="Das Logo wurde nicht gefunden!!"></a>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; <nav id="main-nav">&nbsp; &nbsp; &nbsp; &nbsp; <i class="fa fa-bars" aria-hidden="true">|||</i>&nbsp; &nbsp; &nbsp; &nbsp; <ul>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li><a href="#home"> Lernen </a></li>&nbsp; &nbsp; &nbsp; <li><a href="#was"> Was? </a></li>&nbsp; &nbsp; &nbsp; <li><a href="#fuer-wen"> Für wen? </a></li>&nbsp; &nbsp; &nbsp; <li><a href="#kontakt"> Kontakt </a></li>&nbsp; &nbsp; &nbsp; <li><i class="fas fa-users-cog", id="user-cog"></i></li>&nbsp; &nbsp; &nbsp; &nbsp; </ul>&nbsp; &nbsp; &nbsp; </nav>&nbsp; &nbsp; </header>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript