• 事件的循环绑定


    全部代码:

    <!DOCTYPE HTML">
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
            <title>事件的循环绑定</title>
            <style>
                
                ul {
                    background: #ccc;
                    padding: 10px;
                }
                
                ul li {
                    margin: 15px;
                    background: #84cf39;
                    color: #fff;
                    list-style: none;
                    padding: 5px;
                }
            </style>
            <script type="text/javascript">
                
                var $ = function(elmID){
                    return document.getElementById(elmID);
                }
                window.onload = function(){
                
                    var listOne = $('listOne').getElementsByTagName('li');
                    var listTwo = $('listTwo').getElementsByTagName('li');
                    
                    var resultCon = document.getElementsByTagName('div')[0].getElementsByTagName('span')[0];
                    function Fn(num){
                    
                        this.fn = function(){
                            resultCon.innerHTML = 'listOne-' + num;
                        };
                    }
                    
                    for (var i = 0; i < listOne.length; i++) {
                    
                        var f = new Fn(i);
                        listOne[i].onclick = f.fn;
                    }
                    
                    for (var i = 0; i < listTwo.length; i++) {
                    
                        listTwo[i].onclick = function(){
                            var _i = i;
                            return function(){
                                resultCon.innerHTML = 'listTwo-' + _i;
                            }
                        }();
                        
                    }
                }
            </script>
        </head>
        <body>
            利用new来形成闭包做循环绑定
            <ul id="listOne">
                <li>
                    baby
                </li>
                <li>
                    honey
                </li>
            </ul>
            利用匿名函数形成闭包
            <ul id="listTwo">
                <li>
                    baby
                </li>
                <li>
                    honey
                </li>
            </ul>
            <div>
                result:<span></span>
            </div>
        </body>
    </html>
    

      利用new对象的方法绑定:

                    function Fn(num){
                    
                        this.fn = function(){
                            resultCon.innerHTML = 'listOne-' + num;
                        };
                    }
                    
                    for (var i = 0; i < listOne.length; i++) {
                    
                        var f = new Fn(i);
                        listOne[i].onclick = f.fn;
                    }

    利用闭包的方式绑定

                    for (var i = 0; i < listTwo.length; i++) {
                    
                        listTwo[i].onclick = function(){
                            var _i = i;
                            return function(){
                                resultCon.innerHTML = 'listTwo-' + _i;
                            }
                        }();
                        
                    }
    

      

  • 相关阅读:
    修复ecshop商品重量BUG小数位增至五位
    ECSHOP 支付宝发货确认接口,记录支付宝返回的交易号
    php数字补零的两种方法
    PHP获取当前时间的毫秒数(yyyyMMddHHmmssSSS)
    ajax 设置Access-Control-Allow-Origin实现跨域访问
    MySQL Master High Available 源码篇
    MHA 报错:There is no alive slave. We can't do failover
    cdid
    mha error
    mysql relay log参数汇总
  • 原文地址:https://www.cnblogs.com/longze/p/3363918.html
Copyright © 2020-2023  润新知