[JavaScript] 노드 순서 변경
포스트
취소

[JavaScript] 노드 순서 변경

노드 순서 변경


특정 위치에 노드를 삽입하는 기능
insertBefore(삽입하려는노드, 기준노드) - 기준노드 앞에 삽입노드를 추가

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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        table {border-spacing: 0 ; border-collapse: collapse;}
        thead th, tbody td{
            border: 1px solid black;
        }
    </style>
</head>
<body>
    <table>
        <thead>
            <th>이동</th>
            <th>번호</th>
            <th>이름</th>
            <th>내용</th>
            <th>날짜</th>
        </thead>
        <tbody class="table">
            <tr style="background-color: skyblue;">
                <td><button onclick="down(this)">down</button><button onclick="up(this)">up</button></td>
                <td>1</td>
                <td></td>
                <td>하이</td>
                <td>2023-04-06</td>
            </tr>
            <tr>
                <td><button onclick="down(this)">down</button><button onclick="up(this)">up</button></td>
                <td>2</td>
                <td>chew</td>
                <td>하이</td>
                <td>2023-04-06</td>
            </tr>
            <tr>
                <td><button onclick="down(this)">down</button><button onclick="up(this)">up</button></td>
                <td>3</td>
                <td>brew</td>
                <td>하이</td>
                <td>2023-04-06</td>
            </tr>
            <tr>
                <td><button onclick="down(this)">down</button><button onclick="up(this)">up</button></td>
                <td>4</td>
                <td>java</td>
                <td>하이</td>
                <td>2023-04-06</td>
            </tr>
            <tr>
                <td><button onclick="down(this)">down</button><button onclick="up(this)">up</button></td>
                <td>5</td>
                <td>치치</td>
                <td>하이</td>
                <td>2023-04-06</td>
            </tr>
        </tbody>
    </table>

    <script>
        function down(node){

            var table = document.querySelector(".table");   //table을 선택
            var current = node.parentElement.parentElement; //button 노드 부모의 부모노드 선택 -> tr
            var next = current.nextElementSibling;          //tr의 다음 tr 노드 선택

            if(next === null){                              
                alert("마지막 행입니다.");
                return;
            }
            table.insertBefore(next, current);              //insertBefore(삽입할 노드,기준이되는 노드)
        }                                                   

        function up(node){

            var table = document.querySelector(".table");   
            var current = node.parentElement.parentElement;
            var next = current.previousElementSibling;  

            if(next === null){
                alert("마지막 행입니다.");
                return;
            }

            table.insertBefore(current, next);
        }
    </script>
</body>
</html>
이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.