gpt4 book ai didi

javascript - 如何为隐藏的子菜单应用CSS规则?

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

如何应用隐藏子菜单的CSS规则?我试图添加一些 JS,但有一个问题,因为它不起作用。下面是我处理的代码示例。想法是,在点击 anchor 文本“A”时,应该显示一个子菜单。在此先感谢您的任何建议。

var sub=document.querySelector("#subBox"),
subToggle=document.querySelector("#submenu");
if (subToggle){
subToggle.addEventListener("click",
function(e){
if(sub.className=="open"){
sub.className="";
}else{
sub.className="open";
}
e.preventDefault();
}, false );
}
body {
background: #fff;
font-family: 'Verdana', sans;
color: #fff;
text-align: center;
}

@media only screen and (max-width:768px){
body{font-size:16px;}
}

@media only screen and (min-width:769px){
body{font-size:32px;}
}

ul li{list-style-type: none;}

li{display:inline;}

a, li a{
text-decoration: none;
outline: none;
color:#fff;
}

#menu{
width:100vw;
height:100vh;
display:block;
position:absolute;
top:0;
left:0;
background:#eacebe;
overflow:hidden;
}

#subBox{
max-width:0;
max-height:0;
overflow:hidden;
}

#subBox .open{
width:200px;
height:200px;
display:block;
position:relative;
background:gray;
color:#fff;
}

.skip{
clip: rect(0 0 0 0);
margin: -1px;
overflow:hidden;
display: none;
position: absolute;
top: -1px;
left: -1px;
z-index: -1;
}
<body>
<h1 class='skip'>Exploration of css rules of the hidden submenu</h1>
<div id='nav'>
<nav nav ul>
<h2 class='skip'>Menu with one submenu</h2>
<div id='menu'>
<ul>
<li id='submenu'>
<a href='/a'>A <!--A-->
<div id="subBox">
<ul>
<li><a href='/aOne'>1</a><li> <!--A/1-->
<li><a href='/aTwo'>2</a></li> <!--A/2-->
<li><a href='/aThree'>3</a></li> <!--A/3-->
</ul>
</div>
</a>
</li>
<li><a href='/b'>B</a><li> <!--B-->
<li><a href='/c'>C</a></li> <!--C-->
</ul>
</div>
</nav>
</div>
</body>

最佳答案

你很接近。您正在将 .open 类应用于 #subBox,因此请将您的选择器更改为 #subBox.open

var sub=document.querySelector("#subBox"),
subToggle=document.querySelector("#submenu");
if (subToggle){
subToggle.addEventListener("click",
function(e){
if(sub.className=="open"){
sub.className="";
}else{
sub.className="open";
}
e.preventDefault();
}, false );
}
body {
background: #fff;
font-family: 'Verdana', sans;
color: #fff;
text-align: center;
}

@media only screen and (max-width:768px) {
body {
font-size: 16px;
}
}

@media only screen and (min-width:769px) {
body {
font-size: 32px;
}
}

ul li {
list-style-type: none;
}

li {
display: inline;
}

a,
li a {
text-decoration: none;
outline: none;
color: #fff;
}

#menu {
width: 100vw;
height: 100vh;
display: block;
position: absolute;
top: 0;
left: 0;
background: #eacebe;
overflow: hidden;
}

#subBox {
max-width: 0;
max-height: 0;
overflow: hidden;
}

#subBox.open {
width: 200px;
height: 200px;
display: block;
position: relative;
background: gray;
color: #fff;
overflow: visible;
max-height: 100%;
min-height: 100%;
}

.skip {
clip: rect(0 0 0 0);
margin: -1px;
overflow: hidden;
display: none;
position: absolute;
top: -1px;
left: -1px;
z-index: -1;
}
<body>
<h1 class='skip'>Exploration of css rules of the hidden submenu</h1>
<div id='nav'>
<nav nav ul>
<h2 class='skip'>Menu with one submenu</h2>
<div id='menu'>
<ul>
<li id='submenu'>
<a href='/a'>A <!--A-->
<div id="subBox">
<ul>
<li><a href='/aOne'>1</a>
<li>
<!--A/1-->
<li><a href='/aTwo'>2</a></li>
<!--A/2-->
<li><a href='/aThree'>3</a></li>
<!--A/3-->
</ul>
</div>
</a>
</li>
<li><a href='/b'>B</a>
<li>
<!--B-->
<li><a href='/c'>C</a></li>
<!--C-->
</ul>
</div>
</nav>
</div>
</body>

关于javascript - 如何为隐藏的子菜单应用CSS规则?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42816628/

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