闭包示例

作者: 杰克_王_ | 来源:发表于2019-10-20 15:28 被阅读0次
<!doctype html>
<html lang='en'>

<head>
    <title>闭包示例</title>
    <style>
        .table-normal {
            width: 80%;
            border: 1px solid #ccc;
            border-collapse: collapse;
        }

        .table-normal th,
        .table-normal td {
            padding: 8px;
            border: 1px solid #ccc;
            text-align: left;
        }

        .outBox .checkbox {
            width: 20px;
            height: 20px;
            border: 1px solid #ccc;
            display: inline-block;
            margin-right: 10px;
            cursor: pointer;
        }

        .outBox .checkbox.ok {
            background: url('./images/apply_check_ok_yes_128px_2293_easyicon.net.png') center center no-repeat;
            background-size: 80%;
        }

        .outBox .checkbox.no {
            background: url('./images/close_802px_1161619_easyicon.net.png') center center no-repeat;
            background-size: 80%;
        }
    </style>

</head>
<!-- https://www.easyicon.net -->

<!-- 作业要求: -->
<!-- 1. 尽量用原生的方式, 
2. 不用要let块级作用域 -->

<!-- 提示:我在每个checkbox都加了一个ok样式名, 代表打钩 , 
no代表打叉, 供参考,上面样式 -->

<!-- 作业需求 -->
<!-- 1. 点击每个checkbox, 第一次是打勾, 第二次打叉 , 第三次恢复原来的不选状态
2. 每个checkbox的状态不能相互受影响
3. 点击发送按钮,把获取到的状态 以数组的形式 打印出来 -->

<body>
    <div class="outBox">
        <table class="table-normal">

            <tr>
                <th>姓名</th>
                <th>性别</th>
                <th>年龄</th>
                <th>状态</th>
            </tr>
            <tr>
                <td>小明</td>
                <td>男</td>
                <td>10</td>
                <td><span class="checkbox"></span></td>
            </tr>
            <tr>
                <td>小刚</td>
                <td>男</td>
                <td>10</td>
                <td><span class="checkbox"></span></td>
            </tr>
            <tr>
                <td>小红</td>
                <td>男</td>
                <td>10</td>
                <td><span class="checkbox"></span></td>
            </tr>
            <tr>
                <td>小沙</td>
                <td>男</td>
                <td>10</td>
                <td><span class="checkbox"></span></td>
            </tr>

            <tr>
                <td>小杨</td>
                <td>男</td>
                <td>10</td>
                <td><span class="checkbox"></span></td>
            </tr>

            <tr>
                <td>小松</td>
                <td>男</td>
                <td>10</td>
                <td><span class="checkbox"></span></td>
            </tr>
        </table>

        <!-- <span class="checkbox"></span>
        <span class="checkbox"></span>
        <span class="checkbox"></span>
        <span class="checkbox"></span> -->
    </div>

    <button type="button" id="btnSend">发送</button>
    <script>
        for (var checkboxSpan of document.querySelectorAll('.outBox>.table-normal .checkbox')) {
            // debugger;
            console.log(checkboxSpan);
            (function (checkboxElement) {
                var clickCounter = 0,
                    maxState = 3;

                checkboxElement.onclick = function (e) {
                    clickCounter++;
                    clickCounter = clickCounter % maxState;
                    // debugger;
                    switch (clickCounter) {
                        case 0:
                            e.target.classList.remove('ok')
                            e.target.classList.remove('no');
                            break;
                        case 1:
                            e.target.classList.add('ok')
                            e.target.classList.remove('no');
                            break;
                        case 2:
                            e.target.classList.remove('ok')
                            e.target.classList.add('no');
                            break;
                        default:
                            break;
                    }
                }
            })(checkboxSpan);
        }

        document.getElementById('btnSend').onclick = function (e) {
            var states = [];
            var statesForString = [];
            var stateDictionary = {
                0: "normal",
                1: "ok",
                2: "no"
            };

            for (var checkboxSpan of document.querySelectorAll('.outBox>.table-normal .checkbox')) {
                var state = 0;
                if (checkboxSpan.classList.contains('ok')) {
                    state = 1;
                } else if (checkboxSpan.classList.contains('no')) {
                    state = 2;
                }

                states.push(state);
                statesForString.push(stateDictionary[state]);
            }

            console.log("当前状态: ", states, statesForString);
        }
    </script>
</body>

</html>

相关文章

  • 闭包示例

  • 07-闭包

    闭包表达式(Closure Expression) 闭包表达式的简写 尾随闭包 示例 - 数组的排序 忽略参数 闭...

  • 函数对象和闭包

    函数对象和闭包 一) 函数对象 示例: 二)函数嵌套 三)闭包函数

  • Alamofire 浅析 <五> 使用Operati

    response使用示例: 利用闭包延时求值 result 是一个闭包,只有这个闭包在执行的时候才会获取self....

  • 闭包 vs 对象

    闭包是什么 示例: function counter() { var n = 0; return functi...

  • 详解使用 gradle 构建您的 Android 项目 (F)

    我们看一个闭包应用的示例,这里我们定义一个闭包 doubleIt 其中,使用 lamda 这种表达方式大家应该不会...

  • 通过示例学习JavaScript闭包

    译者按: 在上一篇博客,我们通过实现一个计数器,了解了如何使用闭包(Closure),这篇博客将提供一些代码示例,...

  • 2018-12-22

    1. "闭包就是跨作用域访问变量。" 【示例一】 var name = '小白'; function use...

  • 闭包中的循环引用及解决方法

    闭包中的循环引用 闭包中的循环引用原理和OC中的block类似。即对象A强引用了对象B,然后B也强引用了A。示例如...

  • swift-闭包

    闭包 闭包定义 闭包简化 - 尾随闭包 闭包参数 闭包返回值 闭包的循环引用

网友评论

    本文标题:闭包示例

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