<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <script type="text/javascript"> function makeTable() { var ids = ["aaa","bbb","ccc","ddd"]; var names = ["홍길동","이순신","김유신","강감찬"]; var addrs = ["장성군","한산도","경주","개성"];
var tbody = document.getElementById("tableBody"); //tbody내용을 삭제 //var childCnt = tbody.children.length; var childCnt = tbody.childNodes.length;//childNodes는 텍스트와 엘리먼트 노드가 다 나오지만, 엘리먼트(요소)만 가져오려면 children을 사용해라 for(i=0;i<childCnt;i++){ alert(tbody.firstChild.nodeName); tbody.removeChild(tbody.firstChild); } alert(childCnt);
//추가 for(i=0;i<ids.length;i++){
var trTg = document.createElement("tr"); var td1 = document.createElement("td"); var td2 = document.createElement("td"); var td3 = document.createElement("td"); var id = document.createTextNode(ids[i]); var name = document.createTextNode(names[i]); var addr = document.createTextNode(addrs[i]);