[JavaScript] 노드 생성, 추가와 삭제
포스트
취소

[JavaScript] 노드 생성, 추가와 삭제

노드 생성

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<!DOCTYPE html>
<head>
    <style>
        table {border-spacing: 0; border-collapse: collapse;}
        thread th, tbody td {
            border: 1px solid black;
        }
    </style>
</head>
<body>
    
    <table>
        <thead>
            <tr>
                <th>교사</th>
                <th>훈련과목</th>
                <th>훈련내용</th>
            </tr>
        </thead>

        <tbody class="todoList">
            <tr>
                <td><input type="text" size="3"></td>
                <td><input type="text"></td>
                <td><input type="text"></td>
            </tr>
        </tbody>
    </table>
    <button type="button" id="addList">1개씩 추가하기</button>
    <button type="button" id="addLists">5개씩 추가하기</button>
    <button type="button" id="remove">1개 삭제하기</button>

    <script>
        var todoList = document.querySelector(".todoList");
        var addList = document.getElementById("addList");
        var addLists = document.getElementById("addLists");


        addList.addEventListener("click", function createList(){
            var tr = document.createElement("tr");

            var td = document.createElement("td");
            td.innerHTML = "<input type='text' size='3'>";
            tr.appendChild(td);

            var td = document.createElement("td");
            td.innerHTML = "<input type='text'>";
            tr.appendChild(td);

            var td = document.createElement("td");
            td.innerHTML = "<input type='text'>";
            tr.appendChild(td);

            //todoList 아래에 행을 통째로 추가
           todoList.appendChild(tr);
            
        });


        //addLists.addEventListener("click", createLists)


        addLists.onclick = function(){
            for(var i=0; i<5; i++){            
            createLists();}
        }

        function createLists(){
            var tr = document.createElement("tr");

            var td = document.createElement("td");
            td.innerHTML = "<input type='text' size='3'>";
            tr.appendChild(td);

            var td = document.createElement("td");
            td.innerHTML = "<input type='text'>";
            tr.appendChild(td);

            var td = document.createElement("td");
            td.innerHTML = "<input type='text'>";
            tr.appendChild(td);

            todoList.appendChild(tr);
        }

        function removeList(){
            //todoList.remove(); 테이블을 지워버림
            if(todoList.children.length ===0) return;
            console.dir( todoList )//todoList에서 사용할 수 있는 기능 확인
            todoList.removeChild( todoList.children[0] )
            /*
            console.log(todoList.childNodes)
            console.log(todoList.children)
            console.log(todoList.children[0])
            */
        }
            
        
    </script>
</body>
</html>

이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.