Menu



Manage

파일 목록
Study_Web > 이후/exapmle9-21.html Lines 71 | 1.8 KB
다운로드

                        <!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>키 이벤트 응용</title>
    <style>
        td {
            width: 50px;
            height: 50px;
            border: 1px solid orchid;
        }
    </style>
    <script>
        let tds;
        let prevIndex = 0, index = 0;
        window.onload = function () {
            tds = document.getElementsByTagName("td");
            tds[index].style.backgroundColor = "orchid";
        }
        window.onkeydown = function (e) {
            switch (e.key) {
                case "ArrowDown":
                    if (index / 3 >= 2) return;
                    index += 3;
                    break;
                case "ArrowUp":
                    if (index / 3 < 1) return;
                    index -= 3;
                    break;
                case "ArrowLeft":
                    if (index % 3 == 0) return;
                    index--;
                    break;
                case "ArrowRight":
                    if (index % 3 == 2) return;
                    index++;
                    break;
            }
            tds[index].style.backgroundColor = "orchid";
            tds[prevIndex].style.backgroundColor = "white";
            prevIndex = index;
        }
    </script>
</head>

<body>
    <h3>화살표 키로 셀 이동하기</h3>
    <hr>
    <table>
        <tr>
            <td></td>
            <td></td>
            <td></td>
        </tr>
        <tr>
            <td></td>
            <td></td>
            <td></td>
        </tr>
        <tr>
            <td></td>
            <td></td>
            <td></td>
        </tr>
    </table>
</body>

</html>