Home >Web Front-end >JS Tutorial >How to implement button hiding div in javascript

How to implement button hiding div in javascript

藏色散人
藏色散人Original
2021-07-17 11:43:582555browse

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.

How to implement button hiding div in javascript

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:
How to implement button hiding div in javascript
How to implement button hiding div in javascript

<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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn