我试图切换div以使其可见/隐藏。我正在制作一个我想要打开的排行榜,但只有在点击时才会打开。
<html>
<title>test</title>
<head>
<script type="text/javascript">
<!-- the function -->
function showleaders() {
if (document.getElementById('leaderboard').style.visibility = "hidden";) {
document.getElementById('leaderboard').style.visibility = "visible";
} else {
document.getElementById('leaderboard').style.visibility = "hidden";
}
}
</script>
<style type="text/css">
#leaderboard {
height: 300px;
width:300px;
left:0;
background:red;
border-radius:20%;
visibility:hidden;
}
</style>
</head>
<body>
<a href="javascript:showleaders();">Leaderboard</a>
<div id="leaderboard">hello</div>
</body>
</html>
我做错了什么? 我只想让div在可见性之间切换
答案 0 :(得分:2)
您需要更改此行中的等号:
if(document.getElementById('leaderboard').style.visibility= "hidden";)
以双等于,并删除分号。试试这个:
if(document.getElementById('leaderboard').style.visibility=="hidden")
单个等于设置该值,双等于比较该值。
答案 1 :(得分:0)
试试这个:
function showleaders() {
if (document.getElementById('leaderboard').style.visibility == "hidden") {
document.getElementById('leaderboard').style.visibility = "visible";
} else {
document.getElementById('leaderboard').style.visibility = "hidden";
}
}