Display and hide HTML elements through display or visibility

Display and hide HTML elements through display or visibility
Sometimes we need to control whether HTML elements in a web page are displayed or hidden based on certain conditions. This can be achieved through display or visibility. The following example shows the difference between display and visibility. The simple example code is as follows:

Copy code
The code is as follows:

<html>
<head>
<title>Display and hide control of HTML elements</title>
<script type="text/javascript">
function showAndHidden1(){
var div1 = document.getElementById("div1");
var div2 = document.getElementById("div2");
if(div1.style.display=='block') div1.style.display='none';
else div1.style.display='block';
if(div2.style.display=='block') div2.style.display='none';
else div2.style.display='block';
}
function showAndHidden2(){
var div3 = document.getElementById("div3");
var div4 = document.getElementById("div4");
if(div3.style.visibility=='visible') div3.style.visibility='hidden';
else div3.style.visibility='visible';
if(div4.style.visibility=='visible') div4.style.visibility='hidden';
else div4.style.visibility='visible';
}
</script>
</head>
<body>
<div>display: The element's position is not occupied</div>
<div id="div1" style="display:block;">DIV 1</div>
<div id="div2" style="display:none;">DIV 2</div>
<input type="button" onclick="showAndHidden1();" value="DIV switch" />
<hr>
<div>visibility: The element's position is still occupied</div>
<div id="div3" style="visibility:visible;">DIV 3</div>
<div id="div4" style="visibility:hidden;">DIV 4</div>
<input type="button" onclick="showAndHidden2();" value="DIV switch" />
</body>
</html>

<<:  The benefits of div+css and web standard pages

>>:  Vue implements tree table through element tree control

Recommend

How to use border-image to implement text bubble border sample code

During the development activity, I encountered a ...

Detailed tutorial on installing SonarQube using Docker

Table of contents 1. Pull the image 1.1 Pull the ...

InnoDB type MySql restore table structure and data

Prerequisite: Save the .frm and .ibd files that n...

Let the web page redirect to other pages after opening for a few seconds

Just add the following code to achieve it. Method ...

Detailed explanation of Redis master-slave replication practice using Docker

Table of contents 1. Background 2. Operation step...

How to use Docker to package and deploy images locally

First time using docker to package and deploy ima...

Steps for IDEA to integrate Docker to achieve remote deployment

1. Enable remote access to the docker server Log ...

wget downloads the entire website (whole subdirectory) or a specific directory

Use wget command to download the entire subdirect...

Detailed explanation and summary of the URL for database connection

Detailed explanation and summary of the URL for d...

Forever+nginx deployment method example of Node site

I recently bought the cheapest Tencent cloud serv...

Talking about Less and More in Web Design (Picture)

Less is More is a catchphrase for many designers....

How to query date and time in mysql

Preface: In project development, some business ta...

Detailed installation process and basic usage of MySQL under Windows

Table of contents 1. Download MySQL 2. Install My...

Detailed explanation of the installation steps of the MySQL decompressed version

1. Go to the official website: D:\mysql-5.7.21-wi...

Differences between FLOW CHART and UI FLOW

Many concepts in UI design may seem similar in wo...