<body>
    <p><ol><li>Hello</li></ol></p>
    <p>How are you?</p>
</body><script type="text/javascript" src="jquery-1.3.2.js"></script>
    
    <script type="text/javascript">
        $(document).ready
        (
            function()
            {
                var a=$("p").filter
                (
                    function(index)
                    {
                        return $("ol",this).length==0;
                    }
                );
                alert(a.length);
            }
        );
    </script>请问为什么alert(a.length)出来是3?我觉得应该是1才对呀!

解决方案 »

  1.   

    这是浏览器的bug,这段HTML会被解析成
    <html>
    <head></head>
    <body>
    <p>
    <ol>
    <li>Hello</li>
    </ol>
    <p/>
    <p>How are you?</p>
    </body>
    </html>
      

  2.   

    你那个好像是匹配<p>标签的吧,不是<ol>,,,而且jquery 返回的都是jquery对象
      

  3.   

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>无标题文档</title>
    <script type="text/javascript" src="../jquery-ui-1[1].7.2.custom/js/jquery-1.3.2.min.js"></script>
    <script type="text/javascript">
            $(document).ready
            (
                function()
                {
    alert($("p").length);//最开始p的数量就不对,应该是ol自己会开起一个 段落,因此对ol过滤时可能就出错
    var a = $("p").filter(function(index) {
      return $("span", this).length == 0;
    }); 
    a.css("color","red");
                }
            );
    </script>
    </head><body>
        <p id="p1">p1<ol><li>Hello</li></ol></p>
        <p id="p2">How are you?</p>
    <p id="p2"><span>span</span></p>
    </body>
    </html>
      

  4.   

    我试了一下alert($('body').html());
    出来的居然是这个结果,很是诡异: