zoukankan      html  css  js  c++  java
  • 如何用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>
  • 相关阅读:
    清北学堂(2019 5 3) part 6
    清北学堂(2019 5 2) part 5
    清北学堂(2019 5 1) part 4
    dijkstra
    清北学堂(2019 4 30 ) part 3
    2020/5/1
    2020/4/30
    2020/4/29
    HSV模型
    2020/4/28
  • 原文地址:https://www.cnblogs.com/goldlong/p/7904001.html
Copyright © 2011-2022 走看看