美文网首页
闭包存循环的索引值、闭包做私有变量计数器、闭包做选项卡

闭包存循环的索引值、闭包做私有变量计数器、闭包做选项卡

作者: 栀心_d553 | 来源:发表于2019-12-31 08:14 被阅读0次

闭包存循环的索引值

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>闭包存循环的索引值</title>
    <style type="text/css">
        li{
            height: 30px;
            background-color: gold;
            margin-bottom: 10px;
        }
    </style>
    <script type="text/javascript">
        //闭包的用途:存循环的索引值
        window.onload = function(){
            var aLi = document.getElementsByTagName('li');
            // alert(aLi.length);//8
            for(var i=0; i<aLi.length; i++){
                /*
                aLi[i].onclick = function(){
                    alert(i);//每个li都弹出8,因为点击时循环已完毕,i最后为8
                }
                */
                (function(k){//这里的k是形参
                    aLi[k].onclick = function(){
                        alert(k);//弹出每个li的索引值
                    }
                })(i);//这里的i是实参
            }
        }
    </script>
</head>
<body>
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
        <li>5</li>
        <li>6</li>
        <li>7</li>
        <li>8</li>
    </ul>
</body>
</html>

闭包做私有变量计数器

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>闭包做私有变量计数器</title>
    <script type="text/javascript">
        //闭包的用途:私有变量计数器
        var count = (function(){
            var a = 0;
            function bb(){
                a++;
                return a;
            }
            return bb;
        })();

        //每调用一次count,a就自增一次
        alert(count());//1
        alert(count());//2
        var c = count();
        alert(c);//3
    </script>
</head>
<body>
    
</body>
</html>

闭包做选项卡



<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>闭包做选项卡</title>
    <style type="text/css">
        .btns{
            width: 500px;
            height: 50px;
        }
        /*选项卡的样式*/
        .btns input{
            width: 100px;
            height: 50px;
            background-color: #ddd;/*默认灰色*/
            color: #666;
            border: 0px;
        }
        /*被选中的选项卡的样式*/
        .btns input.cur{
            background-color: gold;
        }
        /*内容区的样式*/
        .contents div{
            width: 500px;
            height: 300px;
            background-color: gold;
            display: none;/*默认隐藏*/
            line-height: 300px;
            text-align: center;
        }
        /*被选中的内容区的样式*/
        .contents div.active{
            display: block;
        }
    </style>
    <script type="text/javascript">
        //闭包做选项卡
        window.onload = function(){
            var aBtn = document.getElementById('btns').getElementsByTagName('input');
            var aCon = document.getElementById('contents').getElementsByTagName('div');
            // alert(aCon.length);
            //循环所有的选项卡按钮
            for(var i=0; i<aBtn.length; i++){
                (function(i){
                    //给每个选项卡按钮添加点击事件
                    aBtn[i].onclick = function(){
                        //遍历所有选项卡按钮
                        for(var j=0; j<aBtn.length; j++){
                            //将每个选项卡按钮都设为灰色
                            aBtn[j].className = '';
                            //将每个内容区都隐藏
                            aCon[j].className = '';
                        }
                        //this代表当前点击的Button对象
                        this.className = 'cur';//当前点击的按钮为金色
                        // alert(i);//不加闭包时,不管点哪个按钮,i都等于3
                        //加闭包保存了索引值才有效
                        aCon[i].className = 'active';//当前点击的按钮对应的内容显示
                    }
                })(i);
            }
        }
    </script>
</head>
<body>
    <div class="btns" id="btns">
        <input type="button" value="tab01" class="cur">
        <input type="button" value="tab02">
        <input type="button" value="tab03">
    </div>
    <div class="contents" id="contents">
        <div class="active">tab文字内容一</div>
        <div>tab文字内容二</div>
        <div>tab文字内容三</div>
    </div>
</body>
</html>

相关文章

  • js闭包的一些用法

    闭包存循环的索引值 闭包做私有变量计数器 闭包做选项卡

  • 闭包应用

    闭包存循环的索引值 闭包做私有变量计数器 闭包做选项卡

  • 闭包存循环的索引值、闭包做私有变量计数器、闭包做选项卡

    闭包存循环的索引值 闭包做私有变量计数器 闭包做选项卡

  • 前端笔记13

    封闭函数 用变量的方式定义函数 闭包 闭包存循环的索引值 闭包做私有变量计数器 闭包做选项卡 跳转原页面 获取地址...

  • Script闭包、构造函数、函数继承

    闭包的作用 1.闭包存循环的索引值 2.闭包做私有变量计数器 3.闭包做选项卡 内置对象 1、document d...

  • 15day-封闭函数闭包

    封闭函数 用变量的方式定义函数 闭包 闭包存循环的索引 闭包做私有变量计数器 (6)闭包做选项卡 (8)跳转的源页面

  • 函数

    封闭函数 用变量的方法定义函数 闭包 闭包存循环的索引值 闭包做私有变量计算器 闭包做选项卡

  • 前端(数组,闭包,定时器)

    数组 定时器 定时器弹框 定时器的基本用法 定时器动画 闭包 闭包存循环的索引值 闭包做私有变量计数器 闭包做选项卡

  • js封闭函数、闭包

    1、封闭函数 2、用变量的方式定义函数 3、闭包 4、闭包存循环的索引值 5、闭包做私有变量计数器 6、闭包做选项...

  • 闭包 函数 选择器 jQuery

    闭包存循环的索引值 闭包做私有变量计算器 闭包做选项卡 跳转的源页面 获取地址栏参数 Math 单体创建对象 工厂...

网友评论

      本文标题:闭包存循环的索引值、闭包做私有变量计数器、闭包做选项卡

      本文链接:https://www.haomeiwen.com/subject/qfzvoctx.html