js经典小案例----乘法表

<style>
        #output span {
            display: inline-block;
            width: 100px;
            height: 40px;
            margin: 5px;
            line-height: 40px;
            text-align: center;
            background-color: #ccc;
        }

        #output span:hover {
            background-color: #fc0;
            color: #fff;
        }
    </style>
</head>

<body>
    <h1>打印99乘法表</h1>
    <div id="output">
       
    </div>
</body>
<script>
  var output = document.getElementById('output');
    var html = '';//准备存放字符串
    for (var i = 1; i <= 9; i++) {
        //外循环:循环行数
        for (var j = 1; j <= i; j++) {
            //内循环:循环列数
            html += '<span>' + i + 'x' + j + '=' + i * j + '</span>';
        }
        html += '<br />';
    }
    output.innerHTML = html;
</script>

适合刚学js的,锻炼一下逻辑

猜你喜欢

转载自www.cnblogs.com/muyun123/p/11404064.html