利用JavaScript的if语句判断元素显示隐藏
<html> <head> <meta charset="utf-8"> <title>无标题文档</title> <style type="text/css"> #div1 { width: 100px; height:100px; background: red; display: none; } </style> <script> function showhide(){ var odiv=document.getElementById('div1'); if(odiv.style.display=='block') { odiv.style.display='none'; } else{ odiv.style.display='block'; } } </script> </head> <body> <input type="button" value="显示隐藏" onClick="showhide()" /> <div id="div1"> </div> </body> </html>
欢迎访问每天进步网:https://meitianjinbu.cn/