执着于理想,纯粹于当下

目录
简单实现闭包
/    

简单实现闭包

之前写过一个关于闭包的小 demo,整理了一下代码,供大家参考,简单易懂。以下是代码和生成的样式

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <style>
        .wrap{
            width: 1100px;
            margin: 0 auto;
            position: relative;
            top: 50px;
        }
            .aa{
               width: 100px;
                height: 100px;
                border-radius:5px;
                background-color: #0ff;
                font-size: 20px;
                color: red;
                display: inline-block;
                vertical-align: middle;
                text-align: center;
            }
    </style>
</head>
<body>
<div class="wrap">
    <div class="aa"></div>
    <div class="aa"></div>
    <div class="aa"></div>
    <div class="aa"></div>
    <div class="aa"></div>
    <div class="aa"></div>
    <div class="aa"></div>
    <div class="aa"></div>
    <div class="aa"></div>
    <div class="aa"></div>
</div>

</body>
<script>
    var divs = document.getElementsByClassName('aa');
    console.log(divs);
    var j=0;
    for(let i = 0;i<10;i++) {
        divs[i].onclick = function () {

            this.innerHTML = i + 1;
            j++;
        }
    }

</script>
</html>


标题:简单实现闭包
作者:JaneChelle
地址:https://xiao.algerfan.cn/articles/2018/11/18/1563541802130.html