我不明白为什么没有通过.toggleClass()
条件。有人能解释一下原因吗?
/**********************
****NAVIGATION****
**********************/
#sidebar {
background: #151718;
width: 12.500em;
height: 100%;
display: block;
position: absolute;
left: -12.500em;
top: 0px;
transition: left 0.3s linear;
}
#sidebar.visible {
left: 0.000em;
}
nav {
text-align: center;
}
ul {
margin: 0.000em;
padding: 0.000em;
}
ul li {
list-style: none;
}
ul li a {
background: #1c1e1f;
color: #ccc;
border-bottom: 1px solid #111;
display: block;
width: 11.250em;
padding: 0.625em;
text-decoration: none;
text-transform: uppercase
}
ul li a:hover {
color: #4876FF;
}
#sidebar-btn {
display: inline-block;
vertical-align: middle;
width: 1.563em;
height: 1.250em;
cursor: pointer;
margin: 1.250em;
position: absolute;
top: 0.000em;
right: -3.750em;
}
#sidebar-btn span {
height: 0.063em;
background: #282828;
margin-bottom: 0.313em;
display: block;
}
输出:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="device-width, initial-scale=1">
<title>Responsive Side Nav</title>
</head>
<body>
<div id="sidebar">
<nav>
<ul>
<li><a href="#" class="active">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
<div id="sidebar-btn">
<span></span>
<span></span>
<span></span>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
window.onload = function() {
document.getElementById('sidebar-btn')
.addEventListener("click", function() {
document.getElementById('sidebar')
.classList.toggle('visible');
});
};
</script>
</body>
</html>
预期输出:
else
如何修复代码?
答案 0 :(得分:3)
这里有两个 if
结构:
if(element[0]=="Test"): # first block
print("[OK]")
if(element[0]=="NO"): # second block
print("[OK]Condition is not verified")
else: # attached to second block
print("[OK]Why the condition is verified??")
element[0]
等于'Test'
,因此它会打印"[OK]"
。然后检查它是否等于其他东西,当然不是,所以然后它转到else
块并打印"[OK]Why the condition is verified??"
。
如果您只想打印一件事,请使用elif
将它们连接在一起:
if element[0]=="Test": # first block
print("[OK]")
elif element[0]=="NO": # attached to first block
print("[OK]Condition is not verified")
else: # still the same block
print("[OK]Why the condition is verified??")