当前位置: 首页 > 图文教程 > 网络编程 > Javascript > Javascript利用循环绑定事件的例子

Javascript
JS 参数传递的实际应用代码分析
prototype与jquery下Ajax实现的差别
用JS写的简单的计算器实现代码
javascript 数组操作实用技巧
JavaScript 中级笔记 第二章
JavaScript 中级笔记 第三章
JavaScript 中级笔记 第四章 闭包
JavaScript 中级笔记 第五章 面向对象的基础
Mootools 1.2教程 函数
Mootools 1.2教程 事件处理
通过Mootools 1.2来操纵HTML DOM元素
Mootools 1.2教程 设置和获取样式表属性
Mootools 1.2教程 输入过滤第一部分(数字)
Mootools 1.2教程 输入过滤第二部分(字符串)
Mootools 1.2教程 Fx.Tween的使用
Mootools 1.2教程 Fx.Morph、Fx选项和Fx事件
MooTools 1.2中的Drag.Move来实现拖放
Mootools 1.2教程 正则表达式
Mootools 1.2教程 定时器和哈希简介
Mootools 1.2教程 滚动条(Slider)

Javascript利用循环绑定事件的例子


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-28   浏览: 381 ::
收藏到网摘: n/a

我们先看一个关于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中最难捉摸的地方之一,

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

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

<!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>
  

PS:闭包很难,很复杂!