childElementCount
1. 예시코드
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Child Element Count Example</title>
</head>
<body>
<div id="parent">
<p>첫 번째 자식 요소</p>
<p>두 번째 자식 요소</p>
<p>세 번째 자식 요소</p>
</div>
<script>
// 특정 태그의 자식 태그 개수를 파악하는 함수
function getChildElementCount(parentId) {
const parentElement = document.getElementById(parentId);
if (parentElement) {
return parentElement.childElementCount;
} else {
console.error('Invalid parent element ID.');
return 0;
}
}
// 'parent' ID를 가진 요소의 자식 요소 개수 파악
const childCount = getChildElementCount('parent');
console.log('자식 요소 개수:', childCount);
</script>
</body>
</html>
2.출력결과
3
반응형
'웹 개발 > 🌐 JavaScript' 카테고리의 다른 글
eChart 메모리 누수 잡는법 (0) | 2024.08.22 |
---|---|
setInterval시 유의사항 (0) | 2024.08.07 |
간단한 로딩기능 구현 (0) | 2024.06.10 |
JS | eCharts 사용시 echarts.js:2286 Can't get DOM width or height. Please check dom.clientWidth and dom.clientHeight. They should not be 0.For example, you may need to call this in the callback of window.onload. 에러 (0) | 2024.04.03 |
JavaScript | localeCompare를 이용한 배열안에서 정렬하는법 (0) | 2024.04.02 |