gpt4 book ai didi

javascript - 切换 JavaScript 不工作

转载 作者:行者123 更新时间:2023-12-03 09:59:59 25 4
gpt4 key购买 nike

我只想要一个简单的切换 js,所以当您在导航栏中单击“关于”时,会出现“关于”文本,但它现在可以正常工作了 oO

JS部分:

<script language="JavaScript" type="text/javascript">
function toggle() {
var about = document.getElementById('about')
if(about.style.display = "none")
{
about.style.display = "block";
}

else
{
about.style.display = "none";
}
}
</script>

HTML:

<header>
...
<nav><ul>
<li><a href="index.html" id="active">Home</a></li>
<li><a href="javascript:toggle()" onClick="toggle()">About me</a></li>
...more <li>s...
</ul></nav>
</header>
<br/>

<div id="about">
aboutme
</div>

CSS:

#about {
display: none;
}

我做错了什么??谁能帮帮我?

最佳答案

在 if 语句中你必须有两个等号

function toggle() {
var about = document.getElementById('about');
if(about.style.display == "none")
{
about.style.display = "block";
}
else
{
about.style.display = "none";
}
}

注意

= you can just assign value to variable
== must be used to compare variables values
=== must be used to compare variables in every aspect (In type too)

关于javascript - 切换 JavaScript 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14304839/

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