我们有几种方式可以隐藏一个元素:
- CSS display的值是none。
- type="hidden"的表单元素。
- 宽度和高度都显式设置为0。
- 一个祖先元素是隐藏的,该元素是不会在页面上显示
- CSS visibility的值是hidden
- CSS opacity的指是0
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title></title>
<link rel="stylesheet" href="imooc.css" type="text/css">
<script src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script>
</head>
<body>
<h2>可见性筛选选择器</h2>
<h3>:visible/:hidden</h3>
<div class="left">
<div class="div">
<a>display</a>
<p id="div1" style="display:none;">display</p>
</div>
<div class="div">
<a>width</a>
<a>height</a>
<p id="div2" style="width:0;height:0">width/height</p>
</div>
<div class="div">
<a>visibility</a>
<a>opacity</a>
<p id="div3" style="visibility:hidden;opacity:0">visibility</p>
</div>
</div>
<p id="show"></p>
<script type="text/javascript">
function show (ele) {
if (ele instanceof jQuery) {
$("#show").html('元素的长度的 = ' + ele.length)
} else {
alert(ele+' 不是jQuery对象')
}
}
</script>
<script type="text/javascript">
//查找id = div1的DOM元素,是否可见
// show($('#div1:visible' ));//0 display:none
//查找id = div1的DOM元素,是否隐藏
// show( $('#div1:hidden' ));//1 display:none
</script>
<script type="text/javascript">
//查找id = div2的DOM元素,是否可见
//show($('#div2:visible'));//0 width:0;height:0
//查找id = div2的DOM元素,是否隐藏
// show( $('#div2:hidden' ));//1 width:0;height:0
</script>
<script type="text/javascript">
//查找id = div3的DOM元素,是否隐藏
// show( $('#div3:hidden' ));//0 visibility:hidden;opacity:0
// 查找id = div3的DOM元素,是否可见
show( $('#div3:visible' ) );//1 visibility:hidden;opacity:0
</script>
</body>
</html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title></title>
<link rel="stylesheet" href="imooc.css" type="text/css">
<script src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script>
</head>
<body>
<h2>可见性筛选选择器</h2>
<h3>:visible/:hidden</h3>
<div class="left">
<div class="div">
<a>display</a>
<p id="div1" style="display:none;">display</p>
</div>
<div class="div">
<a>width</a>
<a>height</a>
<p id="div2" style="width:0;height:0">width/height</p>
</div>
<div class="div">
<a>visibility</a>
<a>opacity</a>
<p id="div3" style="visibility:hidden;opacity:0">visibility</p>
</div>
</div>
<p id="show"></p>
<script type="text/javascript">
function show (ele) {
if (ele instanceof jQuery) {
$("#show").html('元素的长度的 = ' + ele.length)
} else {
alert(ele+' 不是jQuery对象')
}
}
</script>
<script type="text/javascript">
//查找id = div1的DOM元素,是否可见
// show($('#div1:visible' ));//0 display:none
//查找id = div1的DOM元素,是否隐藏
// show( $('#div1:hidden' ));//1 display:none
</script>
<script type="text/javascript">
//查找id = div2的DOM元素,是否可见
//show($('#div2:visible'));//0 width:0;height:0
//查找id = div2的DOM元素,是否隐藏
// show( $('#div2:hidden' ));//1 width:0;height:0
</script>
<script type="text/javascript">
//查找id = div3的DOM元素,是否隐藏
// show( $('#div3:hidden' ));//0 visibility:hidden;opacity:0
// 查找id = div3的DOM元素,是否可见
show( $('#div3:visible' ) );//1 visibility:hidden;opacity:0
</script>
</body>
</html>