document 기본 작업 동적 스크립트 - 동적 스타일 - 표 만들기

2697 단어
var html = document.documentElement;
var body = document.body;

window.onload = function() {
    //document.write("Hello world!");
}
var fragment = document.createDocumentFragment();
var ul = document.getElementById("myDiv");
var li = null;

for (var i = 0; i < 3; i++) {
    li = document.createElement("li");
    li.appendChild(document.createTextNode("Item" + (i + 1)));
    fragment.appendChild(li);
}

ul.appendChild(fragment);

// 
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "docment.js";
document.body.appendChild(script);

//var attr = document.createAttribute("align");
// 

var link = document.createElement("link");
link.rel = "stylesheet";
link.type = "text/css";
link.href = "style.css";

var head = document.getElementsByTagName("head")[0];
head.appendChild(link);

// 
var table = document.createElement("table");
table.border = 1;
table.width = "100%";
// tbody
var tbody = document.createElement("tbody");
table.appendChild(tbody);
// 
var row1 = document.createElement("tr");
tbody.appendChild(row1);

var cell1 = document.createElement("td"); // 
cell1.appendChild(document.createTextNode("cell1 1.1")); // 
row1.appendChild(cell1); // 

var cell2 = document.createElement("td");
cell2.appendChild(document.createTextNode("cell2 2.1"));
row1.appendChild(cell2);

document.body.appendChild(table);


// 

var table = document.createElement("table");
table.border = 1;
table.width = "100%";

var tbody = document.createElement("tbody");
table.appendChild(body);

// 
tbody.insertRow(0);
tbody.rows[0].insertCell(0);
tbody.rows[0].cells[0].appendChild(document.createTextNode("Cell 1,1"));

tbody.rows[0].insertCell(1);
tbody.rows[0].cells[1].appendChild(document.createTextNode("Cell 2,1"));

// 
tbody.insertRow(1);
tbody.rows[1].insertCell(0);
tbody.rows[1].cells[0].appendChild(document.createTextNode("Cell 1,2"));

tbody.rows[1].insertCell(1);
tbody.rows[1].cells[1].appendChild(document.createTextNode("Cell 2,2"));

document.body.appendChild(table);

좋은 웹페이지 즐겨찾기