九九乘法表(br换行/position定位实现)

方法一:<br>标签实现,br换行方式需要设置div样式:display: inline-block;因为div默认会换行显示

注意:innerHTML方法不要子在for循环中循环使用,可以使用变量str接收所有需要生成的HTML内容,再在for循环外一次调用innerHTML。innerHTML属于DOM操作,调用太多次会出现性能问题

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>九九乘法表-br换行方式</title>
    <style>
        body,ul,li,div { margin: 0; padding: 0; }
        img { vertical-align: top; border: none; }
        ul,li { list-style: none; }

        .bg { width: 1600px; height: 1067px; background: url('img/99tab_bg.jpg') no-repeat; margin: 0 auto; }
        .tab99 { width:1156px; height: 436px; background: #e8e5fc; position: relative; top: 436px; left: 250px; padding: 30px 0 0 30px; border-radius: 12px; }
        .tab99 div { width: 120px; height: 40px; line-height: 40px; background: #78cc64; color: white; text-align: center; border-radius: 4px; margin: 0 2px 2px 0; display: inline-block; }
    </style>
</head>
<body>
      <div class="bg">
        <div class="tab99">
          <!-- <div>1 * 1 = 1</div><br><div>1 * 2 = 2</div><div>1 * 2 = 2</div><div>1 * 2 = 2</div> -->
        </div>
      </div>  
      <script>
        //br换行方式需要设置div样式:display: inline-block;
        var colors = ['#78cc64','#58cd7e','#4fcdad','#4cc7dd','#469ee8','#465de8','#5846e8','#8346e8','#9e46e8'];
        var tab99 = document.querySelector(".tab99");
        var divStr = '';
        for(var i=1;i<10;i++){
          for(var j=1;j<=i;j++){
            var txt = j + " * " + i + " = " + i*j;
            divStr += '<div style="background:'+ colors[i-1] +'">'+ txt +'</div>';
          }
          divStr += '<br>';
        }
        tab99.innerHTML = divStr;
      </script>
</body>
</html>

方法二:position方式需要设置div样式:父级相对,自己position绝对定位top,left

注意:根据多个div在行和列的具体位置进行设置left和top的对应值(需要同时考虑width+margin)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>九九乘法表-position定位方式</title>
    <style>
        body,ul,li,div { margin: 0; padding: 0; }
        img { vertical-align: top; border: none; }
        ul,li { list-style: none; }

        .bg { width: 1600px; height: 1067px; background: url('img/99tab_bg.jpg') no-repeat; margin: 0 auto; }
        .tab99 { width:1156px; height: 436px; background: #e8e5fc; top: 436px; left: 250px; padding: 30px 0 0 30px; border-radius: 12px; position: relative; }
        .tab99 div { width: 120px; height: 40px; line-height: 40px; background: #78cc64; color: white; text-align: center; border-radius: 4px; margin: 0 2px 2px 0; position: absolute; }
    </style>
</head>
<body>
      <div class="bg">
        <div class="tab99"></div>
      </div>  
      <script>
        //position方式需要设置div样式:position定位top,left
        var tab99 = document.querySelector(".tab99");
        var divStr = '';
        for(var i=1;i<10;i++){
          for(var j=1;j<=i;j++){
            var txt = j + " * " + i + " = " + i*j;
            //因为i,j初始化为1,但是定位是从0,0开始,所以需要使用i-1和j-1。本身需要上下margin 30所以需要再+30
            var topStr = (i-1)*42 + 30;
            var leftStr = (j-1)*122 + 30;
            divStr += '<div style="top:'+ topStr +'px;left:'+ leftStr +'px;">'+ txt +'</div>';
          }
        }
        tab99.innerHTML = divStr;
      </script>
</body>
</html>

结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值