리자

JS 이용해서 동적으로 테이블 생성

· 18년 전 · 4495
<script>
    function erase(o)
    {
        alert(document.getElementById(o));
        document.getElementById(o).removeChild(document.getElementById(o).getElementsByTagName('tr')[0]);
    }

    function make()
    {
        alert("우하하");
        var tbl     = document.getElementById("tbl");
        var tblBody = document.getElementById("tblBody");

        // creating all cells
        for (var j = 0; j < 2; j++) {
            // creates a table row
            var row = document.createElement("tr");

            for (var i = 0; i < 2; i++) {
                // Create a <td> element and a text node, make the text
                // node the contents of the <td>, and put the <td> at
                // the end of the table row
                var cell = document.createElement("td");
                var cellText = document.createTextNode("cell is row "+j+", column "+i);
                cell.appendChild(cellText);
                row.appendChild(cell);
            }

            // add the row to the end of the table body
            tblBody.appendChild(row);
        }
        tbl.setAttribute("border", "2");
    }
</script>

<input type="button" value="start" onclick="make()">
<input type="button" value="delete" onclick="erase('tblBody');">

<table id="tbl">
    <tbody id="tblBody">


    </tbody>
</table>
 
꼭 tbody 태그를 넣어야 한다는거 유의!!
[이 게시물은 관리자님에 의해 2011-10-31 17:16:08 PHP & HTML에서 이동 됨]
|
댓글을 작성하시려면 로그인이 필요합니다.

개발자팁

개발과 관련된 유용한 정보를 공유하세요. 질문은 QA에서 해주시기 바랍니다.

+
분류 제목 글쓴이 날짜 조회
기타 18년 전 조회 4,251
MySQL 18년 전 조회 1.2만
MySQL 18년 전 조회 6,563
기타 18년 전 조회 3,693
기타 18년 전 조회 2,682
기타 18년 전 조회 3,790
기타 18년 전 조회 9,095
기타 18년 전 조회 3,093
기타 18년 전 조회 5,455
JavaScript 18년 전 조회 3,202
Flash 18년 전 조회 6,039
기타 18년 전 조회 5,040
JavaScript 18년 전 조회 6,110
기타 18년 전 조회 2,999
기타 18년 전 조회 4,496
기타 18년 전 조회 4,270
기타 18년 전 조회 2,739
JavaScript 18년 전 조회 2,526
기타 18년 전 조회 3,046
기타 18년 전 조회 3,723
Flash 18년 전 조회 3,107
Flash 18년 전 조회 3,663
MySQL 18년 전 조회 6,876
기타 18년 전 조회 3,233
PHP 18년 전 조회 4,648
JavaScript 18년 전 조회 1.2만
기타 18년 전 조회 2,927
Flash 18년 전 조회 7,027
기타 18년 전 조회 4,332
JavaScript 18년 전 조회 4,386