DOM으로 테이블 생성하기

1328 단어 html
x.js
function addX(){
    var oTBody = document.createElement("tbody");
    x.appendChild(oTBody);

    var oTr = document.createElement("tr");
    oTBody.appendChild(oTr);

    var oTd1 = document.createElement("td");
    oTd1.appendChild(document.createTextNode("col 1 "));
    oTr.appendChild(oTd1);

    var oTd2 = document.createElement("td");
    oTd2.appendChild(document.createTextNode("col 2"));
    oTr.appendChild(oTd2);
}

function add(){
    rowx = x.rows.length;
    x.insertRow(rowx);
    x.rows[rowx].insertCell(0);
    x.rows[rowx].cells[0].appendChild(document.createTextNode(rowx + 1));
    x.rows[rowx].insertCell(1);
    x.rows[rowx].cells[1].appendChild(document.createTextNode("faint"));
}

 
x.htm
<html>
<head>
<script src="x.js"></script>
</head>
<body>
<input type="button" value=" add " onClick="addX()">
<table id="x" cellpadding="0" cellspacing="0" border="1"></table>
</body>
</html>

 
 
고맙습니다황무지를 줍는 자
 
 

좋은 웹페이지 즐겨찾기