[英]How can I display my list items after clicking an icon?
I coded a dropdown menu for mobile users but it does not work.我为移动用户编写了一个下拉菜单,但它不起作用。 The website is responsive, so you can see the icon for the dropdown menu at a width of 700px.
该网站是响应式的,因此您可以看到宽度为 700 像素的下拉菜单图标。 After clicking the icon, the menu should open and the list items should be displayed in a list on top of each other.
单击图标后,菜单应打开,列表项应显示在彼此顶部的列表中。
I used JavaScript:我使用了 JavaScript:
$(document).ready(function() {
$(".fa-bars").on("click", function() {
$("header nav ul li").toggleClass("open");
});
});
HTML: HTML:
<!DOCTYPE html>
<html lang="de" 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: CSS:
header nav ul li{
display: none;
}
header nav ul li.open{
display: block;
float: none;
padding-top: 10px;
padding-bottom: 10px;
}
When I copy and paste your own code into the editor here, it works.当我在这里将您自己的代码复制并粘贴到编辑器中时,它就可以工作了。 There were a few things about the markup I had to clean up (the
<head>
tag has been duplicated, the body
and html
tag needed to be closed), but that makes me think that the issue is probably not the Javascript or CSS.关于我必须清理的标记有一些事情(
<head>
标记已被复制, body
和html
标记需要关闭),但这让我认为问题可能不是 Javascript 或 Z2C56C3205804285DFBED29
As an unrelated aside, you might consider showing and hiding the ul
instead of all the li
s.顺便说一句,您可以考虑显示和隐藏
ul
而不是所有li
。 Same effect, but fewer things to select and toggle.效果相同,但 select 和切换的东西更少。
$(document).ready(function() { $(".fa-bars").on("click", function() { $("header nav ul li").toggleClass("open"); }); });
header nav ul li{ display: none; } header nav ul li.open{ display: block; float: none; padding-top: 10px; padding-bottom: 10px; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.