ParentNode.childElementCount

ParentNode.childElementCount 읽기 전용 속성은 주어진 요소의 자식 요소 개수를 unsigned long 타입으로 반환합니다.

이 속성은 처음에 ElementTraversal 인터페이스에 정의되었습니다. 이 인터페이스는 자식이 있는 Node와 자식 Node를 위한 두 가지 고유한 속성 집합을 포함하고 있었는데, 각각 ParentNode와 ChildNode 개별 인터페이스로 이동되었습니다. childElementCount의 경우 ParentNode로 이동했습니다. 이것은 기술적인 변화로 호환성에는 영향을 미치지 않습니다.

문법

var count = node.childElementCount;
count
unsigned long(정수) 타입의 반환값.
node
Document, DocumentFragment 또는 Element 객체.

예제

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

폴리필 (IE8 & IE9 & Safari)

이 속성은 IE9 이전 버전에서는 지원하지 않습니다. IE9과 Safari는 Document와 DocumentFragment 객체에서 이 속성을 지원하지 않습니다.

;(function(constructor) {
  if (constructor &&
      constructor.prototype &&
      constructor.prototype.childElementCount == null) {
    Object.defineProperty(constructor.prototype, 'childElementCount', {
      get: function() {
        var i = 0, count = 0, node, nodes = this.childNodes;
        while (node = nodes[i++]) {
          if (node.nodeType === 1) count++;
        }
        return count;
      }
    });
  }
})(window.Node || window.Element);

명세

표준 상태 비고
DOM
The definition of 'ParentNode.childElementCount' in that specification.
Living Standard Split the ElementTraversal interface in ChildNode and ParentNode. This method is now defined on the latter.
The Document and DocumentFragment implemented the new interfaces.
Element Traversal Specification
The definition of 'ElementTraversal.childElementCount' in that specification.
Obsolete Added its initial definition to the ElementTraversal pure interface and use it on Element.

브라우저 호환성

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
childElementCountChrome Full support 1Edge Full support 12Firefox Full support 3.5IE Full support 9Opera Full support 10Safari Full support 4WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
Support on Document and DocumentFragment
Experimental
Chrome Full support 29Edge Full support ≤79Firefox Full support 25IE No support NoOpera Full support 16Safari No support NoWebView Android Full support YesChrome Android Full support YesFirefox Android ? Opera Android ? Safari iOS No support NoSamsung Internet Android Full support Yes

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.

참조