Home > Article > Web Front-end > How to implement button hiding div in javascript
Javascript method to implement button hiding div: first create a front-end sample file; then get the click event based on the id button; and finally add the event handling function.
The operating environment of this article: windows7 system, javascript1.8.5&&html5 version, DELL G3 computer
JavaScript click button to hide and show div Method
Get the click event based on the id button and add the event processing function
Effect:
<style> #dv{ width: 100px; height: 100px; background-color: royalblue; } </style> <body> <input type="button" value="隐藏" id="btn"/> <!--<input type="button" value="显示" id="btn1"/>--> <p id="dv"></p> <script> function my(id){ return document.getElementById(id); } //第一种方法 /*my("btn").onclick=function(){ my("dv").style.display="none"; } my("btn1").onclick=function(){ my("dv").style.display="block"; }*/ //第二种方法 my("btn").onclick=function(){ if (this.value =="隐藏") { my("dv").style.display="none"; this.value="显示"; } else if(this.value =="显示"){ my("dv").style.display="block"; this.value="隐藏"; } } </script> </body>
Recommended study: "javascript Advanced Tutorial"
The above is the detailed content of How to implement button hiding div in javascript. For more information, please follow other related articles on the PHP Chinese website!