js添加表格

2018-09-17  本文已影响0人  MrXuxu
var table = document.getElementById("table");
        table.tHead.style.background = "#ff2d51";
        var rows = table.tBodies[0].rows;
        for(var i=0; i<rows.length;i++){
            if(i%2==0){
                rows[i].style.background = "#eee";
            }else{
                rows[i].style.background = "pink";
            }
        }
        var shop = document.getElementById("shop");
        var cell = document.getElementById("cell");
        var add = document.getElementById("add");
        add.onclick = function(){
            var shopValue = shop.value;
            var cellValue = cell.value;
            var tr = document.createElement("tr");
            var td = document.createElement("td");
            var td1 = document.createElement("td");
            td.innerHTML = shopValue;
            td1.innerHTML = cellValue;
            tr.appendChild(td);
            tr.appendChild(td1);
            table.tBodies[0].appendChild(tr);
            for(var i=0; i<rows.length;i++){
                if(i%2==0){
                    rows[i].style.background = "#eee";
                }else{
                    rows[i].style.background = "pink";
                }
            }
        }
上一篇 下一篇

猜你喜欢

热点阅读