1.设置display:none;
特点: 真正的隐藏元素。
(1) 将元素的display属性设置为none能够确保元素不可见;
(2) 使用这个属性,被隐藏元素不占用任何空间;
(3) 使用display:none隐藏元素,不能直接跟用户进行交互操作(另外,使用读屏软件也不能读取到元素的内容,这种隐藏方式就像元素完全不存在一样);
(4) 任何这个隐藏元素的后代元素也会被隐藏;
(5) 但是,可以通过JS中的DOM操作访问到这个被隐藏的元素,也可以通过DOM对它进行操作。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>元素隐藏的方法一</title> </head> <style> *{ padding: 0; } .outer{ width: 400px; height: 400px; background-color: aquamarine; margin: 0 auto; display: inline-block; } .inner{ width: 200px; height: 200px; background-color: cadetblue; margin: 100px 100px; } </style> <script> window.onload=function(){ var btn=document.getElementsByTagName('button')[0]; var div=document.getElementsByClassName('inner')[0]; btn.onclick=function(){ div.style.display='none'; } } </script> <body> <div class="outer"> <div class="inner"></div> </div> <button >隐藏</button> </body> </html>
2.设置overflow: hidden;
原理: 将元素位置设置到父元素的外面。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>元素隐藏的方法二</title> </head> <style> *{ padding: 0; } .outer{ width: 400px; height: 400px; background-color: aquamarine; margin: 0 auto; display: inline-block; } .inner{ width: 200px; height: 200px; background-color: cadetblue; margin: 100px 500px; //设置到了outer的外面,给outer添加overflow为hidden,inner便会被隐藏 } </style> <script> window.onload=function(){ var btn=document.getElementsByTagName('button')[0]; var div=document.getElementsByClassName('outer')[0]; btn.onclick=function(){ // div.style.overflow='hidden'; } } </script> <body> <div class="outer"> <div class="inner"></div> </div> <button >隐藏</button> </body> </html>
3.设置元素的宽高等盒子模型的属性值为0。
特点: 将元素的宽高设置为0,使用读屏软件仍可以读取到该元素。
<script> window.onload=function(){ var btn=document.getElementsByTagName('button')[0]; var div=document.getElementsByClassName('inner')[0]; btn.onclick=function(){ div.style.height='0'; div.style.width='0'; } } </script>
4.利用定位隐藏元素。
优点:只要通过将元素的left和top设置足够大的数;
缺点:仍然可以使用读屏软件读取元素的内容。
5.opacity:设置元素透明度为0。
特点: 将元素的透明度设置为0,只是单纯的在页面上看不见元素,但是从控制台可以看到该隐藏元素本身的位置仍然存在。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>元素隐藏的方法五</title> </head> <style> *{ padding: 0; } .outer{ width: 400px; height: 400px; background-color: aquamarine; margin: 0 auto; display: inline-block; } .inner{ width: 200px; height: 200px; background-color: cadetblue; margin: 100px 100px; } </style> <script> window.onload=function(){ var btn=document.getElementsByTagName('button')[0]; var div=document.getElementsByClassName('inner')[0]; btn.onclick=function(){ div.style.opacity='0'; //给inner元素的透明度设置为0 } } </script> <body> <div class="outer"> <div class="inner"></div> </div> <button >隐藏</button> </body> </html>
6.visibility: 设置元素是否可见。
默认为:visible(可见);
隐藏(不可见):hidden。
特点: 隐藏元素,但是会保留被隐藏元素的位置。<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>元素隐藏的方法六</title> </head> <style> *{ padding: 0; } .outer{ width: 400px; height: 400px; background-color: aquamarine; margin: 0 auto; display: inline-block; } .inner{ width: 200px; height: 200px; background-color: cadetblue; margin: 100px 100px; } </style> <script> window.onload=function(){ var btn=document.getElementsByTagName('button')[0]; var div=document.getElementsByClassName('inner')[0]; btn.onclick=function(){ div.style.visibility='hidden'; } } </script> <body> <div class="outer"> <div class="inner"></div> </div> <button >隐藏</button> </body> </html>