<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "  http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="  http://www.w3.org/1999/xhtml">
    <head>
        <script src="lib/jquery/jquery-1.3.2.js">
        </script>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>jquery2.1</title>
        <script>
            function showjQueryObjectMethod(){
                var value = $("body *:hidden");
                alert($("body *:hidden").length);
            }
        </script>
    </head>
    <body>
        <input type="hidden" name="ids">
        <div id="hasH1" style="display:none">
            <h1></h1>
            <h1>h2</h1>
            <h1>h2h2</h1>
        </div>
        <div id="hasH2">
            <h1></h1>
            <h1>h2</h1>
            <h1>h2h2</h1>
        </div>
        <div id="nohasH1">
        </div>
        <input type="button" name="button" id="button" value="普通的username名称" onclick="showjQueryObjectMethod()"/>
    </body>
</html>
--------------------------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "  http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="  http://www.w3.org/1999/xhtml">
    <head>
        <script src="lib/jquery/jquery-1.3.2.js">
        </script>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>jquery2.1</title>
        <script>
            function showjQueryObjectMethod(){
                var value = $("body *:visible");
                alert($("body *:visible").length);
            }
        </script>
    </head>
    <body>
        <input type="hidden" name="ids">
        <div id="hasH1" style="display:none">
            <h1></h1>
            <h1>h2</h1>
            <h1>h2h2</h1>
        </div>
        <div id="hasH2">
            <h1></h1>
            <h1>h2</h1>
            <h1>h2h2</h1>
        </div>
        <div id="nohasH1">
        </div>
        <input type="button" name="button" id="button" value="普通的username名称" onclick="showjQueryObjectMethod()"/>
    </body>
</html>
----------------------------------请教啊,,,这个div标签id是nohashH1的到底是隐藏啊,还是显示啊,怎么二段 代码都有它呢?我用firebug调试都有这个,请教一下

解决方案 »

  1.   

    //jQuery
    //:hidden定义
    Sizzle.selectors.filters.hidden = function(elem){
    return elem.offsetWidth === 0 || elem.offsetHeight === 0;
    };
    //:visible定义
    Sizzle.selectors.filters.visible = function(elem){
    return elem.offsetWidth > 0 || elem.offsetHeight > 0;
    };
      

  2.   

    xfsnero感谢您的代码。。
    但为什么隐藏和显示都有nohashH1啊》那它的offsetWidth offsetHeight 到底是什么值啊?
      

  3.   

    offsetHeight offsetWidth 是实际显示的高和宽
    <div id="nohasH1"></div> 当中没有内容
    offsetHeight === 0;
    offsetWidth !== 0;
    所以:hidden和:visible都返回true
      

  4.   

    xfsnero没有内容值是多少呢我常在线
      

  5.   

    offsetHeight offsetWidth 是实际显示的高和宽 
    <div id="nohasH1"> </div> 当中没有内容 
    offsetHeight === 0; 
    offsetWidth !== 0; 
    所以:hidden和:visible都返回true
    ---------您刚才讲的我没有看懂啊,,,,<div id="nohasH1"> </div>中的offsetHeight offsetWidth 值是多少
      

  6.   

    offsetHeight = 0
    offsetWidth = 根据父元素大小
    LZ不是用firebug的吗 可以看的
      

  7.   

    xf我用JS脚本看了一下,,这2个属性全是0为什么visual还能显示出来,,return elem.offsetWidth > 0 || elem.offsetHeight > 0;这段代码是大于0时才选择上啊,为什么是0的时候还被选择了,
      

  8.   

    LZ在firebug的HTML里的“布局”理查看该元素
      

  9.   

    是偏移量吧,是140和8,,,,是这个吗?我的是1280分变率的但我用:alert(document.getElementById('nohasH1').offsetHeight);取出的值是0啊?为什么
      

  10.   

    那按理Sizzle.selectors.filters.visible = function(elem){
        return elem.offsetWidth > 0 || elem.offsetHeight > 0;
    };
    不应该被匹配啊,,,
      

  11.   

    elem.offsetWidth > 0 // true
    elem.offsetHeight > 0 // false
    // true