Hide HTML elements through display or visibility

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>

<<:  Summary of MySQL basic common commands

>>:  Life cycle and hook functions in Vue

Recommend

Steps to install RocketMQ instance on Linux

1. Install JDK 1.1 Check whether the current virt...

React sample code to implement automatic browser refresh

Table of contents What is front-end routing? How ...

Detailed summary of web form submission methods

Let's first look at several ways to submit a ...

What is the use of the enctype field when uploading files?

The enctype attribute of the FORM element specifie...

Nodejs uses readline to prompt for content input example code

Table of contents Preface 1. bat executes js 2. T...

How to configure Linux to use LDAP user authentication

I am using LDAP user management implemented in Ce...

React example of how to get the value of the input box

React multiple ways to get the value of the input...

Hide div in HTML Hide table TABLE or DIV content css style

I solved a problem tonight that has been botherin...

Example code for implementing complex table headers in html table

Use HTML to create complex tables. Complex tables...

Thinking about grid design of web pages

<br />Original address: http://andymao.com/a...

Detailed description of HTML table border control

Only show the top border <table frame=above>...

How to remotely connect to the cloud server database using Navicat

It is very convenient to connect to a remote serv...