ParentNode.childElementCount 只读属性返回一个无符号长整型数字,表示给定元素的子元素数。

This property was initially defined in the ElementTraversal pure interface. As this interface contained two distinct set of properties, one aimed at Node that have children, one at those that are children, they have been moved into two separate pure interfaces, ParentNode and ChildNode. In this case, childElementCount moved to ParentNode. This is a fairly technical change that shouldn't affect compatibility.



var count = node.childElementCount;
var elCount = elementNodeReference.childElementCount; 
holds the return value an unsigned long (simply an integer) type.
is an object representing a DocumentDocumentFragment or Element.


var foo = document.getElementById("foo"); 
if (foo.childElementCount > 0) { 
    // do something


    <script type="text/javascript">
        function GetChildCount () {
            var container = document.getElementById ("container");

            var childCount = 0;
            if ('childElementCount' in container) {
                childCount = container.childElementCount;
            else {
                if (container.children) {
                    childCount = container.children.length;
                else {  //,如果都不支持,Firefox 3.5之前版本
                    var child = container.firstChild;
                    while (child) {
                        if (child.nodeType == 1 /*Node.ELEMENT_NODE*/) {
                        child = child.nextSibling;

            alert ("The number of child elements is " + childCount);
    <div id="container" style="width:300px; background-color:#a0d0e0;">
        Some text inside the container.
        <input type="text" size="40" value="a child element of the container" />
            <div>a descendant element of the container</div>
            <div>another descendant element of the container</div>
    <br /><br />
    <button onclick="GetChildCount ();">Get the number of container's child elements</button>



We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help!

Feature Firefox (Gecko) Chrome Internet Explorer Opera Safari
Basic support Unknown (3) (Yes) 9.0 (Yes) (Yes)
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support ? ? ? ? ?


childElementCount (W3C)




此页面的贡献者: frankfang1990, xgqfrms-GitHub, teoli, AlexChao, ziyunfei
最后编辑者: frankfang1990,