KH 정보교육원/JavaScript

1. 자바스크립트를 이용한 자동 테이블 만들기 - 문제

bameh 2020. 5. 11. 13:21
<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>학생정보 입력</title>
    <style>
        .table{
            border: 1px solid black;
            height: 50px;
            width: 50px;
        }
    </style>
</head>

<body>
    <h1> 자동 테이블 만들기 </h1>
    <h5>힌트 : 반복문은 자바랑 같습니다. 다만! 변수만 var 데이터형이라는걸 기억해주세요 <br>
        document.write()를 연달아 호출하면 이전 document.write() 뒤에 이어서 쓰여집니다.
    </h5>
    <pre>
        document.write("Hello");
        document.write("World");

        [실행결과]
        HelloWorld
    </pre>

    <button onclick="table_make();">테이블 생성</button>

    <script>
        function table_make() {

            var row = window.prompt("행 개수를 입력하세요.")
            var col = window.prompt("열 개수를 입력하세요.")

            document.write("<table border='1px;'>")
            for (var i = 0; i < row; i++) {
                document.write("<tr height='50px'>");
                for (var j = 0; j < col; j++) {
                    document.write("<td width='50px'>");
                }
            }
            document.write("</td>")
            document.write("</tr>")
            document.write("</table>")
        }
    </script>
</body>
</html>