본문 바로가기
2019/JavaScript

JS children & childnode

by SOLYI 2019. 12. 9.

  • children : 자식정보확인
  • childnode : white space포함 정보 확인

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script>
        window.onload=function(){
            let lst=document.getElementsByClassName('menu');
            let chlst=lst[0].children;
            console.log(chlst);
            //li,li,li,li 출력
            console.log(chlst.length);
          
            let chlst2=lst[0].childNodes;
            console.log(chlst2);
            //text, li, text, li, text, li, text, li, text 출력
             
            let chlst3=chlst[3].children;
            console.log(chlst3);
            //ul 출력
            
        }
    </script>
</head>
<body>
    <ul class="menu">
        <li>list-1</li>
        <li>list-2</li>
        <li>list-3</li>
        <li>list-4
            <ul>
                <li>list-4-1</li>
                <li>list-4-2</li>
                <li>list-4-3</li>
            </ul>
        </li>
    </ul>
</body>
</html>
 
반응형

'2019 > JavaScript' 카테고리의 다른 글

JS elementSibling  (0) 2019.12.09
JS addEventListner 이용해서 checkbox 전체선택  (0) 2019.12.09
JS onchange  (0) 2019.12.09
JS addEventListener 연습  (0) 2019.12.09
JS create Element  (0) 2019.12.09