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

border-radius is a method for adding rounded borders to elements

border-radius:10px; /* All corners are rounded wi...

Tutorial on building file sharing service Samba under CentOS6.5

Samba Services: This content is for reference of ...

Bootstrap 3.0 study notes buttons and drop-down menus

The previous article was a simple review of the B...

WeChat applet calculator example

WeChat applet calculator example, for your refere...

5 tips for writing CSS to make your style more standardized

1. Arrange CSS in alphabetical order Not in alphab...

Native js to realize bouncing ball

On a whim, I wrote a case study of a small ball b...

How to monitor oracle database using zabbix agent2

Overview In zabbix version 5.0 and above, a new f...

How to insert Emoji expressions into MySQL

Preface Today, when I was designing a feedback fo...

Solution to multiple 302 responses in nginx proxy (nginx Follow 302)

Proxying multiple 302s with proxy_intercept_error...

The difference between this.$router and this.$route in Vue and the push() method

The official document states: By injecting the ro...

Analysis of SQL integrity constraint statements in database

Integrity constraints Integrity constraints are f...

Docker packages the local image and restores it to other machines

1. Use docker images to view all the image files ...

CSS3 uses animation attributes to achieve cool effects (recommended)

animation-name animation name, can have multiple ...