闭包

作者: AnnaJIAN | 来源:发表于2018-12-06 21:49 被阅读0次
    <div id="divTest">
            <span>0</span> <span>1</span> <span>2</span> <span>3</span>
        </div>
        <div id="divTest2">
            <span>0</span> <span>1</span> <span>2</span> <span>3</span>
        </div>
    
    $(document).ready(function() {
                var spans = $("#divTest span");
                for (var i = 0; i < spans.length; i++) {
                    spans[i].onclick = function() {
                        alert(i);
                    }
                }
            });
    
    var spans2 = $("#divTest2 span");
            $(document).ready(function() {
                for (var i = 0; i < spans2.length; i++) {
                    (function(num) {
                        spans2[i].onclick = function() {
                            alert(num);
                        }
                    })(i);
                }
            });
    

    相关文章

      网友评论

          本文标题:闭包

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