<!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="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script> </head> <body> <h2>可见性筛选选择器</h2> <h3>:visible/:hidden</h3> <div class="left"> <div class="div"> <a>display</a> <p id="div1" style="display:block;">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) { //instanceof监测类型 $("#show").html('元素的长度的 = ' + ele.length) } else { $("#show").html(ele+' 不是jQuery对象') } } </script> <script type="text/javascript"> //查找id = div1的DOM元素,是否可见 show( $(".div1:visible") ); //元素的长度的 = 0 //display:none; </script> <script type="text/javascript"> //查找id = div2的DOM元素,是否可见 show( $(".div2:visible") ); //元素的长度的 = 0 //width:0;height:0 </script> <script type="text/javascript"> //查找id = div3的DOM元素,是否可见 show( $(".div3:visible") ); //元素的长度的 = 0 //visibility:hidden;opacity:0 </script> <script type="text/javascript"> //查找id = div1的DOM元素,是否隐藏 show( $("#div1:hidden") ); //元素的长度的 = 1 </script> <script type="text/javascript"> //查找id = div2的DOM元素,是否隐藏 show( $("#div2:hidden") ); //元素的长度的 = 1 </script> <script type="text/javascript"> //查找id = div3的DOM元素,是否隐藏 show( $("#div:hidden") ); //元素的长度的 = 0 </script> </body> </html>
为何<p id="div1" style="display:block;">display</p>这里的display已经设置成block了,但是元素的长度还是等于0。
div1是class。用#div1不是.div1