单击图标后如何显示我的列表项?
How can I display my list items after clicking an icon?
我为移动用户编写了一个下拉菜单,但它不起作用。
该网站是响应式的,因此您可以看到宽度为 700 像素的下拉菜单图标。
单击该图标后,应打开菜单,列表项应显示在彼此顶部的列表中。
我用了JavaScript:
$(document).ready(function() {
$(".fa-bars").on("click", function() {
$("header nav ul li").toggleClass("open");
});
});
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:
header nav ul li{
display: none;
}
header nav ul li.open{
display: block;
float: none;
padding-top: 10px;
padding-bottom: 10px;
}
当我将您自己的代码复制并粘贴到此处的编辑器中时,它起作用了。我必须清理一些关于标记的内容(<head>
标签已重复,body
和 html
标签需要关闭),但这让我觉得问题可能不是 Javascript 或 CSS.
顺便说一句,您可以考虑显示和隐藏 ul
而不是所有 li
。效果相同,但 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>
我为移动用户编写了一个下拉菜单,但它不起作用。 该网站是响应式的,因此您可以看到宽度为 700 像素的下拉菜单图标。 单击该图标后,应打开菜单,列表项应显示在彼此顶部的列表中。
我用了JavaScript:
$(document).ready(function() {
$(".fa-bars").on("click", function() {
$("header nav ul li").toggleClass("open");
});
});
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:
header nav ul li{
display: none;
}
header nav ul li.open{
display: block;
float: none;
padding-top: 10px;
padding-bottom: 10px;
}
当我将您自己的代码复制并粘贴到此处的编辑器中时,它起作用了。我必须清理一些关于标记的内容(<head>
标签已重复,body
和 html
标签需要关闭),但这让我觉得问题可能不是 Javascript 或 CSS.
顺便说一句,您可以考虑显示和隐藏 ul
而不是所有 li
。效果相同,但 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>