zoukankan      html  css  js  c++  java
  • js闭包Demo

    我们先看一个关于Javascript利用循环绑定事件的例子:

    例如:一个不确定长度的列表,在鼠标经过某一条的时候改变背景。

     
    ﹤!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"﹥   
    ﹤html xmlns="http://www.w3.org/1999/xhtml" ﹥   
    ﹤head﹥   
    ﹤title﹥Untitled Page﹤/title﹥   
    ﹤/head﹥   
    ﹤body﹥   
    ﹤ul id="list"﹥   
    ﹤li﹥第1条记录﹤/li﹥   
    ﹤li﹥第2条记录﹤/li﹥   
    ﹤li﹥第3条记录﹤/li﹥   
    ﹤li﹥第4条记录﹤/li﹥   
    ﹤li﹥第5条记录﹤/li﹥   
    ﹤li﹥第6条记录﹤/li﹥   
    ﹤/ul﹥   
    ﹤script type="text/javascript"﹥   
    var list_obj = document.getElementById("list").getElementsByTagName("li"); //获取list下面的所有li的对象数组    
    for (var i = 0; i ﹤= list_obj.length; i++) {    
    list_obj[i].onmousemove = function() {    
    this.style.backgroundColor = "#cdcdcd";    
    }    
    list_obj[i].onmouseout = function() {    
    this.style.backgroundColor = "#FFFFFF";    
    }    
    }    
    ﹤/script﹥   
    ﹤/body﹥   
    ﹤/html﹥

    这个例子循环为一组对象绑定事件处理函数。

    但是,如果我们在这个基础上增加一些需求。比如在点击某一条记录的时候弹出这是第几条记录?

    可能你会理所当然的这么写:

     

    ﹤!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"﹥   
    ﹤html xmlns="http://www.w3.org/1999/xhtml" ﹥   
    ﹤head﹥   
    ﹤title﹥Untitled Page﹤/title﹥   
    ﹤/head﹥   
    ﹤body﹥   
    ﹤ul id="list"﹥   
    ﹤li﹥第1条记录﹤/li﹥   
    ﹤li﹥第2条记录﹤/li﹥   
    ﹤li﹥第3条记录﹤/li﹥   
    ﹤li﹥第4条记录﹤/li﹥   
    ﹤li﹥第5条记录﹤/li﹥   
    ﹤li﹥第6条记录﹤/li﹥   
    ﹤/ul﹥   
    ﹤script type="text/javascript"﹥   
    var list_obj = document.getElementById("list").getElementsByTagName("li"); //获取list下面的所有li的对象数组    
    for (var i = 0; i ﹤= list_obj.length; i++) {    
    list_obj[i].onmousemove = function() {    
    this.style.backgroundColor = "#cdcdcd";    
    }    
    list_obj[i].onmouseout = function() {    
    this.style.backgroundColor = "#FFFFFF";    
    }    
    list_obj[i].onclick = function() {    
    alert("这是第" + i + "记录");    
    }    
    }    
    ﹤/script﹥   
    ﹤/body﹥   
    ﹤/html﹥

    测试一下你会发现alert出来的都是:这是第6记录

    其实这里for循环已将整个列表循环了一遍,并执行了i++,所以这里i变成了6,

    有什么好的办法解决这个问题吗?

    那就是闭包了,个人认为闭包是js中最难捉摸的地方之一,

    看看什么是闭包:

    闭包时是指内层的函数可以引用存在与包围他的函数内的变量,即使外层的函数的执行已经终止。

    可以查阅:http://www.css88.com/article.asp?id=469

    这个例子中我们可以这样做:


    ﹤!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"﹥   
    ﹤html xmlns="http://www.w3.org/1999/xhtml" ﹥   
    ﹤head﹥   
    ﹤title﹥Untitled Page﹤/title﹥   
    ﹤/head﹥   
    ﹤body﹥   
    ﹤ul id="list"﹥   
    ﹤li﹥第1条记录﹤/li﹥   
    ﹤li﹥第2条记录﹤/li﹥   
    ﹤li﹥第3条记录﹤/li﹥   
    ﹤li﹥第4条记录﹤/li﹥   
    ﹤li﹥第5条记录﹤/li﹥   
    ﹤li﹥第6条记录﹤/li﹥   
    ﹤/ul﹥   
    ﹤script type="text/javascript"﹥   
    function tt(nob) {    
    this.clickFunc = function() {    
    alert("这是第" + (nob + 1) + "记录");    
    }    
    }    
    var list_obj = document.getElementById("list").getElementsByTagName("li"); //获取list下面的所有li的对象数组    
    for (var i = 0; i ﹤= list_obj.length; i++) {    
    list_obj[i].onmousemove = function() {    
    this.style.backgroundColor = "#cdcdcd";    
    }    
    list_obj[i].onmouseout = function() {    
    this.style.backgroundColor = "#FFFFFF";    
    }    
    var col = new tt(i);    
    list_obj[i].onclick = col.clickFunc;    
    }    
    ﹤/script﹥   
    ﹤/body﹥   
    ﹤/html﹥

     摘自:http://www.cnblogs.com/angells/archive/2009/11/19/1606439.html 

  • 相关阅读:
    【leetcode】1442. Count Triplets That Can Form Two Arrays of Equal XOR
    【leetcode】1441. Build an Array With Stack Operations
    【leetcode】1437. Check If All 1's Are at Least Length K Places Away
    cxCheckCombobox
    修改现有字段默认值
    2018.01.02 exprottoexcel
    Statusbar OwnerDraw
    dxComponentPrinter记录
    单据暂存操作思路整理
    设置模式9(装饰者,责任链,桥接,访问者)
  • 原文地址:https://www.cnblogs.com/coikeizeon/p/3809611.html
Copyright © 2011-2022 走看看