js实现N行M列表格,并填充0~100随机数

编写一个函数,在页面上输出一个N行M列的表格,表格内容填充0~100的随机数字

通过调用函数生成一个5行8列随机数表格
在这里插入图片描述

 <script>
        // 编写一个函数,在页面上输出一个N行M列的表格,表格内容填充0~100的随机数字
        function printTable(n,m){
    
    
            document.write("<table>")
                for(var i=0; i <= n; i++){
    
    
                    document.write("<tr>")
                        for(var j = 0; j < m; j++){
    
    
                            document.write("<td>")
                            document.write(Math.round(Math.random()*100));
                            //生成0-100随机数(含小数),再四舍五入取整数
                            document.write("</td>")
                        }
                    document.write("</tr>")
                }
            document.write("</table>")
        }
        printTable(5,8);
        //调用函数,生成5行5列表格
    </script>
    <style>
        table tr td{
    
    
            width: 100px;height: 50px;
        }
    </style>

猜你喜欢

转载自blog.csdn.net/horizon12/article/details/108243821