简体   繁体   English

单击javascript / html后如何切换div

[英]How to toggle a div after clicking javascript/html

I'm trying to use a div to show game rules, so when they select the "button" it will reveal the rules, but I can't get it to close by reselecting it. 我正在尝试使用div来显示游戏规则,因此当他们选择“按钮”时,它将显示规则,但是我无法通过重新选择规则来使其关闭。 I have tried a few other solutions but they are not quite what I am looking for. 我尝试了其他一些解决方案,但它们并不是我想要的。

<script type="text/javascript">
function showDiv() {
   document.getElementById('howto').style.display = "block";
}
</script>

<input type="button" name="rules" value="How to Play!" onclick="showDiv()" />
<div id="howto"  style="display:none;" class="rules" > WELCOME</div>

You could have a toggleDiv rather than showDiv function that checks the display style and sets it to whichever of block or none it is not currently set to. 您可以使用toggleDiv而不是showDiv函数来检查显示样式并将其设置为当前未设置的none blocknone block For example: 例如:

<script type="text/javascript">
function toggleDiv() {
 var div = document.getElementById('howto'); 
 div.style.display = (div.style.display == "block") ? "none" : "block";
}
</script>

<input type="button" name="rules" value="How to Play!" onclick="toggleDiv()" />
<div id="howto"  style="display:none;" class="rules" > WELCOME</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM