gpt4 book ai didi

即使 :hover and display block has been set 也没有显示 html 下拉菜单

转载 作者:太空宇宙 更新时间:2023-11-04 09:42:03 24 4
gpt4 key购买 nike

我是网页设计和堆栈溢出的新手。我注意到在 stack overflow 上有一个与我的问题类似的问题,但我的问题没有在那里得到解答,所以我发布了我自己的问题,希望你能帮忙!我一直在尝试制作一个下拉菜单,它不仅会出现,还会插入其下的元素移动。不过,我现在主要关心的是下拉菜单。我添加了position:relative于父级并设置:悬停以显示下拉列表但没有结果。这是我所有的代码:

function openNav() {
document.getElementById("mySidenav").style.width = "250px";
document.getElementById("main").style.marginLeft = "250px";
document.getElementById("nav").style.marginLeft = "250px";
}

function closeNav() {
document.getElementById("mySidenav").style.width = "0";
document.getElementById("main").style.marginLeft = "0";
document.getElementById("nav").style.marginLeft = "0";
}
body {
font-family: "Lato", sans-serif;
}
.sidenav {
height: 100%;
width: 0;
position: fixed;
z-index: 1;
top: 0;
left: 0;
background-color: crimson;
overflow-x: hidden;
transition: 0.5s;
padding-top: 80px;
}
.sidenav a {
padding: 8px 8px 8px 32px;
text-decoration: none;
font-size: 25px;
color: white;
display: block;
transition: 0.3s;
}
.sidenav #menu-list:hover,
.offcanvas a:focus {
color: crimson;
background-color: white;
}
.sidenav a:hover,
.offcanvas a:focus {
color: white;
}
.sidenav .closebtn {
position: absolute;
top: 0;
right: 25px;
font-size: 36px;
margin-left: 50px;
}
#menu-list {
padding-top: 17px;
padding-bottom: 17px;
text-align: center;
padding-right: 33px;
}
#main {
transition: margin-left .5s;
padding: 16px;
padding-top: 55px;
left: 0;
right: 0;
top: 0;
width: 100%;
}
#nav {
transition: .5s;
padding-top: 15px;
padding-bottom: 15px;
padding-left: 6%;
background-color: white;
position: fixed;
z-index: 1;
width: 100%;
}
#nav-element {
text-align: left;
width: 120px;
display: inline;
color: crimson;
}
#red-logo {
margin-left: 50px;
font-size: 30px;
color: crimson;
}
#heading {
font-size: 45px;
}
@media screen and (max-height: 450px) {
.sidenav {
padding-top: 15px;
}
.sidenav a {
font-size: 18px;
}
}
li {
list-style-type: none;
}
#dropdown {
display: none;
width: 40px;
}
#dropdown a {
color: white;
display: block;
}
.test {
position: relative;
}
a .test:hover > #dropdown {
display: block;
}
<!DOCTYPE html>
<html>

<head>
<title>Red Storm News</title>
<link rel="stylesheet" href="http://www.w3schools.com/lib/w3.css">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>

<body>
<div id="mySidenav" class="sidenav">
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
<a id="menu-list" href="/redstormtv/redstormtv.html">Red Strom TV</a>
<a id="menu-list" class="test">The News
<ul id="dropdown"> #What i'm trying be a dropdown
<li><a href="/schoolnews/schoolnews.html">School News</a>
</li>
<li><a href="/schoolsports/schoolsports.html">School Sports</a>
</li>
<li><a href="/worldnews/worldnews.html">World News</a>
</li>
</ul>
</a>
<a id="menu-list" href="/archives/archives.html">Archives</a>
<a id="menu-list" href="/partners/partners.html">Partners</a>
</div>
<div class="container" id="nav">
<div id="nav-element">
<span style="font-size:30px;cursor:pointer" onclick="openNav()">&#9776; Menu</span>
</div>
<div id="nav-element">
<a id="red-logo" href="index.html">Red Strom News</a>
</div>
</div>
<div class="container" id="main">
#type text here#
</div>

</body>

</html>

提前致谢!

最佳答案

这里的问题是嵌套的 anchor 标签。您试图将下拉菜单放在“The News”的“a”标签中,但下拉菜单有自己的 A 标签,这是非法的。参见 this question获取更多信息。

相反,将 anchor 标记和下拉列表包裹在一个 div 中。

<!DOCTYPE html>
<html>
<style>
body
{
font-family: "Lato", sans-serif;
}

.sidenav
{
height: 100%;
width: 0;
position: fixed;
z-index: 1;
top: 0;
left: 0;
background-color: crimson;
overflow-x: hidden;
transition: 0.5s;
padding-top: 80px;
}

.sidenav a
{
padding: 8px 8px 8px 32px;
text-decoration: none;
font-size: 25px;
color: white;
display: block;
transition: 0.3s;
}

.sidenav #menu-list:hover, .offcanvas a:focus
{
color: crimson;
background-color: white;
}

.sidenav a:hover, .offcanvas a:focus
{
color: white;
}

.sidenav .closebtn
{
position: absolute;
top: 0;
right: 25px;
font-size: 36px;
margin-left: 50px;
}

#menu-list
{
padding-top: 17px;
padding-bottom: 17px;
text-align: center;
padding-right: 33px;
}

#main
{
transition: margin-left .5s;
padding: 16px;
padding-top: 55px;
left: 0;
right: 0;
top: 0;
width: 100%;
}

#nav
{
transition: .5s;
padding-top: 15px;
padding-bottom: 15px;
padding-left: 6%;
background-color: white;
position: fixed;
z-index: 1;
width: 100%;
}

#nav-element
{
text-align: left;
width: 120px;
display: inline;
color: crimson;
}

#red-logo
{
margin-left: 50px;
font-size: 30px;
color: crimson;
}

#heading
{
font-size: 45px;
}

@media screen and (max-height: 450px)
{
.sidenav {padding-top: 15px;}
.sidenav a {font-size: 18px;}
}

li
{
list-style-type: none;
}

#dropdown
{
display: none;
width: 40px;
}

#dropdown a
{
color: white;
display: block;
}

.test
{
position: relative;
}

.test:hover > #dropdown
{
display: block;
}
</style>

<head>
<title>Red Storm News</title>
<link rel="stylesheet" href="http://www.w3schools.com/lib/w3.css">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>

<body>
<div id="mySidenav" class="sidenav">
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
<a id="menu-list" href="/redstormtv/redstormtv.html">Red Strom TV</a>
<div class="test">
<a id="menu-list">The News</a>
<ul id="dropdown"> #What i'm trying be a dropdown
<li><a href="/schoolnews/schoolnews.html">School News</a></li>
<li><a href="/schoolsports/schoolsports.html">School Sports</a></li>
<li><a href="/worldnews/worldnews.html">World News</a></li>
</ul>
</div>
<a id="menu-list" href="/archives/archives.html">Archives</a>
<a id="menu-list" href="/partners/partners.html">Partners</a>
</div>
<div class="container" id="nav">
<div id="nav-element">
<span style="font-size:30px;cursor:pointer" onclick="openNav()">&#9776; Menu</span>
</div>
<div id="nav-element">
<a id="red-logo" href="index.html">Red Strom News</a>
</div>
</div>
<div class="container" id="main">
#type text here#
</div>
<script>
function openNav() {
document.getElementById("mySidenav").style.width = "250px";
document.getElementById("main").style.marginLeft = "250px";
document.getElementById("nav").style.marginLeft = "250px";
}

function closeNav() {
document.getElementById("mySidenav").style.width = "0";
document.getElementById("main").style.marginLeft= "0";
document.getElementById("nav").style.marginLeft = "0";
}
</script>
</body>

关于即使 :hover and display block has been set 也没有显示 html 下拉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39813265/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com