如何用js創建表格?


1.用js創建表格

<script>
function createTable(){
//創建表格
//創建對象
//window下面的屬性方法可以把window去掉或者寫上
var table = window.document.createElement("table");
for(var i=0;i<4;i++){
var tr = document.createElement("tr");
var td = document.createElement("td");

var txt = document.createTextNode("hello");
var txt2 = document.createTextNode("hello");
//把表格添加到body里去
td.appendChild(txt);
td.appendChild(txt2);
tr.appendChild(td);


//給表格設置各行變色
if(i%2 == 0){

//偶數為紅色
tr.style.backgroundColor = "red";
}else{

//奇數為綠色
tr.style.backgroundColor = "green";
}
table.appendChild(tr);
}
table.setAttribute("border",1);

document.body.appendChild(table);
}
createTable();

</script>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM