本文實例為大家分享了js實現隔行變色的具體代碼,供大家參考,具體內容如下
<script type="text/javascript"> function createTable(){ //創建表格 var table = document.createElement("table"); //設置表格的行數 for(var i=0;i<4;i++){ var tr = document.createElement("tr"); //設置表格的列數 for( var j=0;j<5;j++){ var td = document.createElement("td"); tr.appendChild(td); //添加文本 var txt = document.createTextNode("wkk"); td.appendChild(txt); } //判斷顏色的變換 if(i%2==0){ //添加屬性(第一種) tr.style.backgroundColor = "#f0f"; } else { tr.style.backgroundColor = "#ff0"; } table.appendChild(tr); } //添加屬性(第二種) table.setAttribute("border","1px"); //table.style.border = "solid 1px red"; table.setAttribute("width","200px"); table.setAttribute("height","100px"); //添加到body中 document.body.appendChild(table); }//create table over createTable(); </script>
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com