Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 是否有JS/jQuery完全递归的子节点计数函数?_Javascript_Jquery_Recursion_Count_Children - Fatal编程技术网

Javascript 是否有JS/jQuery完全递归的子节点计数函数?

Javascript 是否有JS/jQuery完全递归的子节点计数函数?,javascript,jquery,recursion,count,children,Javascript,Jquery,Recursion,Count,Children,各位 假设我有以下情况: <div class="outer"> <div> <span> <ul> <div> </div> <div> </div> </ul> </span> </div>

各位

假设我有以下情况:

<div class="outer">
    <div>
        <span>
             <ul>
                 <div> </div>
                 <div> </div>
             </ul>
         </span>
     </div>
  </div>


如何找到div.outer中的元素总数?类似于$('div.outer').children().size()或length返回1(在本例中,我查找的是5)是否有使用js/jQuery查找元素总数的快捷方式或函数,或者我是否应该为此问题编写一个函数?TIA.

var TotalDescents=$('div.outer*')。长度

完成了



var totalsubjects=$('div.outer').find('*').length

也可以使用。

一个简单(可能比jQuery更快)的JavaScript替代方案是

var el = document.getElementById("foo");
var descendantElementCount = el.getElementsByTagName("*").length;
这假设您已经为元素提供了一个id。如果这很重要,您可以使用jQuery获取类
外部
的所有元素:

var descendantElementCount = 0;
$('div.outer').each(function() {
    descendantElementCount += this.getElementsByTagName("*").length;
});

jQuery递归子节点计数函数:

jQuery(文档).ready(函数(){
var totalChildren=jQuery(“*”,“div.outer”).length;

});

太棒了。谢谢你的及时答复。