我只想要一个简单的切换js,所以当你点击导航中的About时会出现about文本,但它现在正在工作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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
CSS:
#about {
display: none;
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么??有谁能够帮我?
在if语句中你必须 have two equal sign
function toggle() {
var about = document.getElementById('about');
if(about.style.display == "none")
{
about.style.display = "block";
}
else
{
about.style.display = "none";
}
}
Run Code Online (Sandbox Code Playgroud)
注意
= 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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
152 次 |
| 最近记录: |