以下代码用于切换开启和关闭的可见性,但我希望能够隐藏任何可见的div并仅显示可见的div。这是HTML:
<div id="hiddena">
</div>
<div id="hiddenb">
</div>
<div id="hiddenc">
</div>
<div id="hiddend">
</div>
<div id="hiddene">
</div>
</div>
<div id="buttona">
<button type=submit onclick="switchVisible('hiddena');"></button>
</div>
<div id="buttonb">
<button type=submit onclick="switchVisible('hiddenb');"></button>
</div>
<div id="buttonc">
<button type=submit onclick="switchVisible('hiddenc');"></button>
</div>
<div id="buttond">
<button type=submit onclick="switchVisible('hiddend');"></button>
</div>
<div id="buttone" class="tall">
<button type=submit onclick="switchVisible('hiddene');"></button>
</div>
这是脚本......
function switchVisible(id) {
var e = document.getElementById(id);
if(e.style.display == 'block')
e.style.display = 'none';
else
e.style.display = 'block';
}
</script>
是否有人对脚本有任何建议或补充可以实现此目的?
由于
答案 0 :(得分:1)
一次隐藏所有这些,然后只出现你想要的那个会不会有问题?
function hide_them() {
var divsList = ["hiddena", "hiddenb", "hiddenc", "hiddend", "hiddene"]
divsList.forEach(function (element) {
element.style.visibility = "hidden";
});
}
最后,只需在你的func开始时调用hide_them函数:
function switchVisible(id) {
hide_them();
var e = document.getElementById(id);
e.style.display = 'block';
}
另一种方法是按标签选择元素,并将它们全部隐藏(这不是很好的方法,因为当你的代码长大时,它可能会导致不必要的问题)
答案 1 :(得分:0)
这里很少有建议
检查以下代码段
<div id="hiddena">
div1
</div>
<div id="hiddenb">
div2
</div>
<div id="hiddenc">
div3
</div>
<div id="hiddend">
div4
</div>
<div id="hiddene">
div5
</div>
<div id="buttona">button1
<button type=submit onclick="switchVisiblebyId('hiddena');"></button>
</div>
<div id="buttonb">button2
<button type=submit onclick="switchVisiblebyId('hiddenb');"></button>
</div>
<div id="buttonc">button3
<button type=submit onclick="switchVisiblebyId('hiddenc');"></button>
</div>
<div id="buttond">button4
<button type=submit onclick="switchVisiblebyId('hiddend');"></button>
</div>
<div id="buttone" class="tall">button5
<button type=submit onclick="switchVisiblebyId('hiddene');"></button>
</div>
{{1}}