<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>Document</title>
</head>
<style>
    input{
        height: 30px;
        width: 80px;
        border: 2px solid #ccc;
    }
    .btn{
        color: #fff;
        background: #000;
        border-radius: 6px;
        border: 0;
    }
    .tabstyle{
        border: 1px solid #000;
    }
    .tdstyle{
        border: 1px solid #000;
        height: 30px;
        width: 50px;
        line-height: 30px;
        text-align: center;
    }
</style>
<body>
    <div class="getinput" id="getinput">
        行数<input type="text" />
        列数<input type="text" />
        <input type="button" value="生成表格" class="btn"/>
    </div>
    <div class="box" id="box">

    </div>

</body>
    <script>
        //获取元素
        var getinput=document.getElementById("getinput");
        var getTag=getinput.getElementsByTagName("input");
        var box=document.getElementById("box");
        //点击事件的实现
        getTag[2].onclick=function(){
            //获取用户输入的行数和列数
            var rowCound=parseInt(getTag[0].value);
            var colCound=parseInt(getTag[1].value);
            //创建表格节点
            var createTab=document.createElement("table");
            createTab.className="tabstyle";
            box.appendChild(createTab);

            for(var i=0;i<rowCound;i++){
                //创建行节点
                var createTr=document.createElement("tr");
                createTab.appendChild(createTr);
                for(var j=0;j<colCound;j++){
                    //创建列节点
                    var createTd=document.createElement("td");
                    createTd.className="tdstyle";
                    //随机生成每个表格中的内容
                    var ranNum=parseInt(Math.random()*15);
                    createTd.innerHTML=ranNum;
                    //随机生成表格的背景颜色
                    rg=parseInt(Math.random()*255);
                    gg=parseInt(Math.random()*255);
                    bg=parseInt(Math.random()*255);
                    createTd.style.background="rgb("+rg+","+gg+","+bg+")";
                    createTr.appendChild(createTd);
                }
            }
        }
    </script>
</html>

posted on 2016-08-30 21:06  悠悠风来  阅读(1027)  评论(0编辑  收藏  举报