<html>
<head>
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
$(document).ready(function () {
var array = [0, 1, 2, 3]; // 1.
/*
for(var index in array) {
$("#btn" + index).click(function() {
var item = array[index];
alert(item);
});
}*/
// 始终弹出3, 因为function() {} 并没有被立即解析,直到调用的时候才被解析,这时index已经是3了。 // 2.
/*
for(var index in array) {
$("#btn" + index).click(function(i) {
var item = array[i];
alert(item);
}(index));
}*/
// 立即弹出0, 1, 2, 3,因为使用了function() {}(index)立即被解析,遇到alert,就立即弹出来了。 // 3.
/*for (var index in array) {
$("#btn" + index).click(function (i) {
return function () {
var item = array[i];
alert(item);
};
} (index));
}*/
// 正确执行,点击btn0,弹出0,点击btn1,弹出1...
// 1.因为function(i) {}(index)是被立即解析的,所以i依次送入的是0, 1, 2, 3
// 2.内部没有直接alert,是因为不想立即执行,想点击时再执行,所以返回了一个函数出去。 // 4.
for (var index in array) {
$("#btn" + index).bind("click", {index: index}, clickHandler);
} function clickHandler(event) {
var index = event.data.index;
var item = array[index];
alert(item);
}
// 正确执行,点击btn0,弹出0,点击btn1,弹出1...
// 利用了event.data,因为index在绑定的时候已经被持久化到event.data中了,所以响应的时候我们可以取到。
}); </script> <input type="button" id="btn0" value="btn0" />
<input type="button" id="btn1" value="btn1" />
<input type="button" id="btn2" value="btn2" />
<input type="button" id="btn3" value="btn3" />
</body>
</html>

jquery循环绑定事件的更多相关文章

  1. JavaScript利用闭包循环绑定事件

    我们经常在做前端面试题的时候,会遇到循环绑定事件后,输出打印结果,很多人总是搞不清楚,今天借此机会跟大家梳理一下闭包相关作用. 1.首先我们举一个简单的例子. html部分: <a href=& ...

  2. jquery on 绑定事件

    jquery on 绑定事件 1. 多个选择器绑定一个事件 2. 多个事件绑定一个函数 3. 一个选择器绑定多个事件,有两种写法: 或者 on只绑定一次事件,绑定父元素,防止初始化时数据未加载,绑定出 ...

  3. jQuery中绑定事件bind() on() live() one()的异同

    jQuery中绑定事件的四种方法,他们可以同时绑定一个或多个事件 bind()-------------------------版本号小于3.0(在Jquery3.0中已经移除,相应unbind()也 ...

  4. jQuery中绑定事件的几种方法

    以click事件为例,jQuery中绑定事件有三种方法: (1)target.click(function(){});  (2)target.bind("click",functi ...

  5. JQuery中绑定事件(bind())和移除事件(unbind())

    有时候事件执行完了,想取消事件的效果可以通过一定的办法来处理.比如bind()(绑定事件)和unbind()(移除通过bind()方法添加的事件)方法来移除事件的效果. 比如下面的一个案例: 复制代码 ...

  6. jquery on绑定事件叠加解决方法

    jquery on绑定事件叠加解决方法 代码如下 <pre> $('.maoqiu').each(function () { var is_bind = $(this).attr('is_ ...

  7. dom元素循环绑定事件的技巧

    以前总觉得自己写的代码不太规范,尤其是写原生的时候.举个例子: 要为页面上所有".a"的元素绑定事件,当然了用jquery很方便:$('.a').bind("click& ...

  8. jQuery .on() 绑定事件无效

    前几天,要在移动端实现一系列的功能,用 HTML + JS. 按照以往的思路,事件绑定就直接 $(document).on "click", "selector" ...

  9. jquery 获取绑定事件

    在1.8.0版本之前,我们要想获取某个DOM绑定的事件处理程序可以这样: 1 $.data(domObj,'events');//或者$('selector').data('events') 而从1. ...

随机推荐

  1. JS 中的foreach和For in比较

    使用方式举例如下: <script type="text/javascript"> var jsonranklist=[{"name":" ...

  2. C# 常用正则表达式

    // 匹配移动手机号 @"^1(3[4-9]|5[012789]|8[78])\d{8}$"; // 匹配电信手机号   @"^18[09]\d{8}$";   ...

  3. WCF:2个常见错误

      1.另一应用程序已使用 HTTP.SYS 注册了该 URL 在做WCF wsDualHttpBinding的时候,调试时会出现此异常. 其意思为:有一个Host已经启动了,占用了指定的端口了. & ...

  4. 在visual studio中使用git版本系统(zz)

    第一部分: 安装 git 开发工具 如果要使用 git 进行版本管理,其实使用 git 命令行工具就完全足够了,图形化工具(无论是 git extentions ,还是TortoiseGit),都只不 ...

  5. Color the Ball[HDU1199]

    Color the Ball Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  6. BZOJ3672 : [Noi2014]购票

    设d[i]表示i到1的距离 f[i]=w[i]+min(f[j]+(d[i]-d[j])*v[i])=w[i]+d[i]*v[i]+min(-d[j]*v[i]+f[j]) 对这棵树进行点分治,每次递 ...

  7. BZOJ3867 : Nice boat

    每个点最多被修改$O(\log n)$次,线段树记录区间最值暴力更新. #include<cstdio> #define N 262145 int T,n,m,i,op,c,d,p,s[N ...

  8. const 与指针

    深入理解const char*p,char const*p,char *const p,const char **p,char const**p,char *const*p,char**const p ...

  9. Sqlserver 存储过程中结合事务的代码

    Sqlserver 存储过程中结合事务的代码  --方式一 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[ ...

  10. _jobdu_1001

    /************************************************************************/ /* 题目描述: This time, you a ...