zoukankan      html  css  js  c++  java
  • js实现表格的随机生成

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8" />
        <title>Document</title>
    </head>
    <style>
        input{
            height: 30px;
             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;
             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>

  • 相关阅读:
    jqgard改变单元格后重新定值(事件和弹窗)
    js多个input框赋相同值
    查看PHP已安装拓展的指令
    PHP重新安装zlib拓展,处理PHP Startup: Invalid library (maybe not a PHP library) 'zlib.so' in Unknown
    php拓展安装报错:PHP Startup: Invalid library (maybe not a PHP library) 'zlib.so' in Unknown
    Composer提示:Installation Failed, Reverting ./Composer.Json To Its Original Content.错误的解决办法
    SQL Server序列号的获取
    一步步开发Windows服务(Windows Service)[转]
    HTML+CSS+JS实现的贪吃球小游戏【转】
    自制一个滚动条
  • 原文地址:https://www.cnblogs.com/html-go/p/5823627.html
Copyright © 2011-2022 走看看