From 8d7ad7715e19a445def22de6d5f3d9519cebe69e Mon Sep 17 00:00:00 2001 From: MDN Date: Wed, 24 Mar 2021 00:27:57 +0000 Subject: [CRON] sync translated content --- .../web/api/element/childelementcount/index.html | 153 +++++++++++++++++++++ 1 file changed, 153 insertions(+) create mode 100644 files/zh-cn/web/api/element/childelementcount/index.html (limited to 'files/zh-cn/web/api/element') diff --git a/files/zh-cn/web/api/element/childelementcount/index.html b/files/zh-cn/web/api/element/childelementcount/index.html new file mode 100644 index 0000000000..11a680d8a0 --- /dev/null +++ b/files/zh-cn/web/api/element/childelementcount/index.html @@ -0,0 +1,153 @@ +--- +title: ParentNode.childElementCount +slug: Web/API/Element/childElementCount +translation_of: Web/API/ParentNode/childElementCount +original_slug: Web/API/ParentNode/childElementCount +--- +

{{ APIRef("DOM") }} 

+ +

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

+ +
+

This property was initially defined in the {{domxref("ElementTraversal")}} pure interface. As this interface contained two distinct set of properties, one aimed at {{domxref("Node")}} that have children, one at those that are children, they have been moved into two separate pure interfaces, {{domxref("ParentNode")}} and {{domxref("ChildNode")}}. In this case, childElementCount moved to {{domxref("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
+ +

 

+ +

浏览器兼容性

+ +

{{ CompatibilityTable() }}

+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureFirefox (Gecko)ChromeInternet ExplorerOperaSafari
Basic support{{ CompatGeckoDesktop("3") }}{{ CompatVersionUnknown() }}9.0{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}
+
+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureAndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support?????
+
+ +

规范

+ +

childElementCount (W3C)

+ +

相关链接

+ + + +

 

-- cgit v1.2.3-54-g00ecf