JavaScript를 사용하여 요소의 자식 요소 수를 얻는 방법은 무엇입니까?
2876 단어 javascript
상위 요소 내부의 하위 요소 수를 얻으려면 다음을 사용할 수 있습니다.
childElementCount
JavaScript의 DOM 요소에 있는 속성.
다음과 같이 내부에 일부
div
요소가 있는 paragraph
요소가 있다고 가정해 보겠습니다.<div>
<p>I'm a Paragraph</p>
<p>I'm a Paragraph</p>
<p>I'm a Paragraph</p>
<p>I'm a Paragraph</p>
<p>I'm a Paragraph</p>
<p>I'm a Paragraph</p>
<p>I'm a Paragraph</p>
<p>I'm a Paragraph</p>
<p>I'm a Paragraph</p>
<p>I'm a Paragraph</p>
<p>I'm a Paragraph</p>
<p>I'm a Paragraph</p>
<p>I'm a Paragraph</p>
</div>
이제
paragraph
요소의 childElementCount
속성을 사용하여 자식div
요소의 수를 알아보겠습니다.// get reference to the div element
const div = document.querySelector("div");
// get the length of children
// inside the div element
const lengthOfChildren = div.childElementCount;
console.log(lengthOfChildren); // 13
JSBin에 있는 이 예제를 참조하세요.
😃 유용하셨다면 공유해 주세요.
Reference
이 문제에 관하여(JavaScript를 사용하여 요소의 자식 요소 수를 얻는 방법은 무엇입니까?), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/melvin2016/how-to-get-the-count-of-child-elements-of-an-element-using-javascript-5ak4텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)