动态生成表格

这段时间疯狂的刷前端。。。。。。。。。。。。。。。。。

<div id="bottomTable">
</div>
//页面中只要加上一个div就行了,直接掉这个方法会创建一个表格,row为要创建的行数,col为要创建的列数
function creatTab(row,col){
        rows= row;
        cols=col;
        div1=document.getElementById('bottomTable');
        var ta=div1.getElementsByTagName('table')[0]
        if (ta) {ta.parentNode.removeChild(ta)}
        var tab = document.createElement("table");
        tab.setAttribute("id","lwmxTab");
        tab.border=1;
        // 设置表格宽度
        tab.style.width="100%";
        // 设置表格高度
        tab.style.height="80px";  
        // 设置表格中文字居中展示
        tab.style.cssText += 'text-align:center';
        for(var i=0;i<=rows;i++){
            tab.insertRow(i)
            for(var j=0;j<=cols;j++){
            	tab.rows[i].insertCell(j);
            }
        }
        document.getElementById("bottomTable").appendChild(tab)
    }

猜你喜欢

转载自blog.csdn.net/weixin_39144798/article/details/83584873
今日推荐