ParentNode.childElementCount - Web API 接口参考 编辑

 

 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;
count
holds the return value an unsigned long (simply an integer) type.
node
is an object representing a DocumentDocumentFragment or Element.

例子

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

下例演示了一个元素节点的childElementCount属性以及children属性的用法.

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

            var childCount = 0;
            //如果支持childElementCount属性
            if ('childElementCount' in container) {
                childCount = container.childElementCount;
            }
            else {
            //如果支持children属性,IE6/7/8
            //译者注:没有判断nodeType是否为1,可能包含注释节点.
                if (container.children) {
                    childCount = container.children.length;
                }
                else {  //,如果都不支持,Firefox 3.5之前版本
                    var child = container.firstChild;
                    while (child) {
                        if (child.nodeType == 1 /*Node.ELEMENT_NODE*/) {
                            childCount++;
                        }
                        child = child.nextSibling;
                    }
                }
            }

            alert ("The number of child elements is " + childCount);
        }
    </script>
</head>
<body>
    <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>
            <div>a descendant element of the container</div>
            <div>another descendant element of the container</div>
        </div>
    </div>
    <br /><br />
    <button onclick="GetChildCount ();">Get the number of container's child elements</button>
</body>
执行上面的例子:http://help.dottoro.com/external/examples/ljsfamht/childElementCount_1.htm

 

浏览器兼容性

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!
FeatureFirefox (Gecko)ChromeInternet ExplorerOperaSafari
Basic supportUnknown (3)(Yes)9.0(Yes)(Yes)
FeatureAndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support?????

规范

childElementCount (W3C)

相关链接

 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:62 次

字数:7386

最后编辑:7年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文