首頁  >  文章  >  web前端  >  js怎麼實現隔行變色

js怎麼實現隔行變色

php中世界最好的语言
php中世界最好的语言原創
2018-04-14 11:55:033231瀏覽

這次帶給大家js怎麼實現隔行變色,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>

我相信看了本文案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!

推薦閱讀:

easyui的下拉框動態級聯載入的實作方法(附程式碼)

透過JS取得JSON資料並載入的步驟詳解

#

以上是js怎麼實現隔行變色的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn