前端js小练习,动态生成乘法表

输入数字,生成对应乘法表,表格线也有要求

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" href="css/style.css">
    <style>
/*        .body{width: 10px;height: 10px;}*/
/*        #bo{width: 10px;height: 10px;}*/
    </style>
</head>
<body id="bo">
    <div>
    N=
        <input type="text" id="txt1" >
        <input type="button" value="button" onclick="btn1_click()" >
        <table  id="tb" style="border-collapse: collapse"  border="0">
        <tbody>

        </tbody>
<!--
            <tr>
                <td>1</td>
                <td>2</td>
                <td>3</td>
            </tr>
            <tr>
                <td>4</td>
                <td>5</td>
                <td>6</td>
            </tr>
            <tr>
                <td>7</td>
                <td>8</td>
                <td>9</td>
            </tr>
-->
        </table>
    </div>
<!--
    <span style="background: red">
        AAA
    </span>
    <input type="checkbox" name="a" id="chk1">
    <input type="radio" name="a" id="r1">
    <input type="radio" name="a" id="r2">
    <input type="radio" name="a" id="r3">
    <input type="radio" name="a" id="r4">
    <select name="b" id="s1">
        <option value="A">ads</option>
        <option value="B">asd</option>
        <option value="C">ads</option>
    </select>
-->
</body>
<script>
//    window.onload=function(){alert("hello")}  
    function btn1_click(){
//        alert("he")
        var a=document.getElementById("txt1").value;
        var f=isNaN(a);
        if(!f){
//            window.open("https://www.baidu.com")
            var tb=document.getElementById("tb");
            tb.innerHTML=""
             var trt=document.createElement("tr");
            for(var i=0;i<=parseInt(a);i++){
//                tb.innerHTML+="<tr>";
                var trt=document.createElement("tr");
                tb.appendChild(trt);
                if(i==0)trt.setAttribute("style","border-bottom: 1px solid #000")
//                trt.style.border="1px solid #000";
//                trt.border='0';
                for(var j=0;j<=parseInt(a);j++){
//                    tb.innerHTML+="<td>"+i*j+"</td>"
                    var tdt=document.createElement("td");
                    trt.appendChild(tdt);
                    tdt.width="50"
                    if(j==0){tdt.setAttribute("style","border-right: 1px solid #000");tdt.innerHTML=i;continue;}
                    if(i==0){tdt.innerHTML=j;continue;}
                    tdt.innerHTML=i*j;
                }
//                tb.innerHTML+="</tr>";
            }
        }
    }
</script>
</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值