gpt4 book ai didi

javascript - 我的 javascript 触发器无法在我的代码中使用 css 媒体查询

转载 作者:行者123 更新时间:2023-11-27 23:51:55 26 4
gpt4 key购买 nike

我正在创建一个网络元素。我想在 JavaScript 中切换隐藏显示。

<nav id="navbar" class="sticky">
<div id="logo"><a href="Javascript: xs();" class="lounch" id="xs">&#9776;</a><a href="Javascript: sm();" class="lounch" id="sm">&#9776;</a><a href="index"><img src="./logo/logo.png" alt=""></a></div>
<div class="links">
<div class="link ac"><a href="index" class="active">index</a></div>
<div class="link"><a href="Javascript: alrt();">about</a></div>
<div class="link"><a href="Javascript: alrt();">explore</a></div>
<div class="link"><a href="Javascript: alrt();">article</a></div>
<div class="link"><a href="Javascript: alrt();">contact</a></div>
</div>
<div class="connect">
<a href="signin">sign in</a>
<a href="signup">sign up</a>
</div>
</nav>

这是我的触发按钮。

<div id="logo"><a href="Javascript: xs();" class="lounch" id="xs">&#9776;</a><a href="Javascript: sm();" class="lounch" id="sm">&#9776;</a><a href="index"><img src="./logo/logo.png" alt=""></a></div>

这是触发器,适用于小屏幕。我想在单击 #xs 时隐藏/显示 .links

.links的Scss代码

.links{
display: flex;
align-items: center;
margin: auto;
.link{
margin: 10px;
align-items: center;
a{
text-transform: uppercase;
padding: 7px;
border: 2px solid;
}
a.active,a:hover{
color: $bd;
background: $lgd;
}
a.active:hover{
color: $lgl;
background: none;
}
}
}
@media screen and (max-width: 700px){
.links{
display: block;
text-align: center;
margin: 0;
margin-top: -18px;
padding-top: 3px;
border-bottom: 2px solid;
border-right: 2px solid;
width: 170px;
.link{
padding: 6px 20px;
align-items: center;
border-bottom: 1px solid;
margin: auto;
a.active:hover,a:hover,a.active,a{
text-transform: uppercase;
padding: 6px 60px 6px 10px;
margin: auto;
border: 2px solid;
color: $lgd;
background: none;
border:none;
}
}
.ac{
background: $lgd;
a.active:hover,a:hover,a.active,a{
background: none;
color: $bd;
}
}
.link:last-child{
border-bottom: 0;
}
}
}

我的javascript代码

document.getElementById('xs').addEventListener('click', function() {
let val = document.getElementByClassName('content').style.display == 'block' ? 'none' : 'block';
document.getElementById('content').style.display = val;
});
document.getElementById('sm').addEventListener('click', function() {
let val = document.getElementByClassName('content').style.display == 'block' ? 'none' : 'block';
document.getElementById('content').style.display = val;
let vals = document.getElementByClassName('connect').style.display == 'block' ? 'none' : 'block';
document.getElementById('connect').style.display = vals;
});

我想要一个触发器来显示/隐藏.links

最佳答案

这是一个在 View 中切换可见性的非常简单的示例。

document.getElementById('toggle').addEventListener('click', function() {
let val = document.getElementById('content').style.display == 'block' ? 'none' : 'block';
document.getElementById('content').style.display = val;
})
<button id="toggle">Click me</button>
<div id="content" style="display: block">This div can be hidden</div>

关于javascript - 我的 javascript 触发器无法在我的代码中使用 css 媒体查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56479830/

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