jQuery事件绑定on、off 和one,取代bind, live, delegate
jQuery最新版建议:最好用on来代替以前的bind, live, delegate,其中live是最不建议使用的。
on和off的格式
on
$(elements).on(events[, selector][, data],handler);//格式一
$(elements).on(eventsMap[, selector][, data]); //格式二
例如:
$('#container a').on({
click: function(){
event.preventDefault();
console.log('item anchor clicked');
},
mouseenter: function(){
console.log('enter!');
}
});
off
$(elements).off(events[,selector][, handler] );
one
$(elements).one(events[, selector][, data],handler);//在每个对象上,这个事件处理函数只会被执行一次。可应用于后添加的元素
on代替bind,delegate,live
bind
// old way - .bind(events, handler);
$('#container a').bind('click',function(event){
event.preventDefault();
console.log('item anchor clicked');
});
// new way (jQuery 1.7+) - on(events, handler);
$('#container a').on('click',function(event){
event.preventDefault();
console.log('item anchor clicked');
});
live
// do not use! - .live(events, handler)
$('#container a').live('click',function(event){
event.preventDefault();
console.log('item anchor clicked');
});
// new way (jQuery 1.7+) - .on(events, selector, handler)
$('#container').on('click','a', function(event){
event.preventDefault();
console.log('item anchor clicked');
});
delegate
// old way - .delegate(selector, events, handler);
$('#container').delegate('a','click',function(event){
event.preventDefault();
console.log('item anchor clicked');
});
// new way (jQuery 1.7+) - on(events, selector, handler);
$('#container').on('click','a', function(event){
event.preventDefault();
console.log('item anchor clicked');
});
off代替unbind,undelegate,die
// old way - .bind(events);
$('#container a').unbind('click');
// new way (jQuery 1.7+) - off(events);
$('#container a').off('click');
$('#container a').off('click',handleClick);//取消绑定单独函数
// do not use! - .die(events)
$('#container a').die('click');
// new way (jQuery 1.7+) - .on(events, selector, handler)
$('#container').off('click','a');
// old way - .undelegate(selector, events);
$('#container').undelegate('a','click');
// new way (jQuery 1.7+) - off(events, selector);
$('#container').off('click','a');
jQuery事件绑定on、off 和one,取代bind, live, delegate的更多相关文章
- 深入学习jQuery事件绑定
× 目录 [1]bind [2]trigger [3]delegate[4]on[5]one 前面的话 javascript有HTML.DOM0级.DOM2级和IE这四种事件处理程序,而jQuery对 ...
- jQuery事件绑定、解绑、命名空间
jQuery事件绑定.解绑.命名空间 <%@ page language="java" import="java.util.*" pageEncoding ...
- jQuery 事件绑定 和 JavaScript 原生事件绑定
总结一下:jQuery 事件绑定 和 JavaScript 原生事件绑定 及 区别 jQuery 事件绑定 jQuery 中提供了四种事件监听绑定方式,分别是 bind.live.delegate.o ...
- JQuery事件绑定,bind与on区别
jquery事件绑定bind:向匹配元素添加一个或多个事件处理器 $(selector).bind("click",data,function); live:向当前或未来的匹配元素 ...
- jQuery事件绑定和委托实例
本文实例讲述了jQuery事件绑定和委托.分享给大家供大家参考.具体方法如下: jQuery事件的绑定和委托可以用多种方法实现,on() . bind() . live() . delegate ...
- jquery事件绑定方式总结(补充)
总结 : 1.简单事件绑定方式:事件名() 如:click() 2.高级事件绑定方式:bind(事件名,数据参数,function) 3.动态生成元素事件绑定方式:live(事件名,数据参数, ...
- jquery事件绑定函数
1.bind 使用语法: jQueryObject.bind( events [, data ], handler ) jQueryObject.bind( events [, data ] [, i ...
- javascript事件委托和jQuery事件绑定on、off 和one以及on绑定多个事件(重要)
一. 事件委托什么是事件委托?用现实中的理解就是:有100 个学生同时在某天中午收到快递,但这100 个学生不可能同时站在学校门口等,那么都会委托门卫去收取,然后再逐个交给学生.而在jQuery 中, ...
- jQuery事件绑定.on()简要概述及应用
原文地址:http://www.jquerycn.cn/a_5346 前几天看到事件委托的时候,关于live()方法讲的不是很详细,就去搜了一下关于live()和delegate()的,最后看 ...
随机推荐
- 扩展KMP --- HDU 3613 Best Reward
Best Reward Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=3613 Mean: 给你一个字符串,每个字符都有一个权 ...
- Manacher算法 - 求最长回文串的利器
求最长回文串的利器 - Manacher算法 Manacher主要是用来求某个字符串的最长回文子串. 不要被manacher这个名字吓倒了,其实manacher算法很简单,也很容易理解,程序短,时间复 ...
- office2016 软件全集 官方下载免费完整版(含破解文件)不含垃圾软件 win10完美激活
office2016官方下载免费完整版是新一代办公软件,office2016官方下载免费完整版已经分享到下面,office2016官方下载免费完整版包括了Word.Excel.PowerPoint.O ...
- C#写文本日志帮助类(支持多线程)
代码: using System; using System.Configuration; using System.IO; using System.Threading.Tasks; namespa ...
- [新手学Java]反射学习笔记
示例类 @SuppressWarnings("unused") public class Person { public String Name; private int Age; ...
- JavaScript语言知识收藏
接触Web开发也已经有一段时间了,对javascript的认识也比以前有了更加深入的认识了,所以觉得应该整理一下. 一.JavaScript不支持函数(方法)的重载,用一个例子证明如下: functi ...
- 重新想象 Windows 8 Store Apps (60) - 通信: 获取网络信息, 序列化和反序列化
[源码下载] 重新想象 Windows 8 Store Apps (60) - 通信: 获取网络信息, 序列化和反序列化 作者:webabcd 介绍重新想象 Windows 8 Store Apps ...
- 重新想象 Windows 8.1 Store Apps 系列文章索引
[源码下载] [重新想象 Windows 8 Store Apps 系列文章] 重新想象 Windows 8.1 Store Apps 系列文章索引 作者:webabcd 1.重新想象 Windows ...
- 一、MyBatis简介与配置MyBatis+Spring+MySql
//备注:该博客引自:http://limingnihao.iteye.com/blog/106076 1.1MyBatis简介 MyBatis 是一个可以自定义SQL.存储过程和高级映射的持久层框架 ...
- Java 上传文件到 SFTP 抛异常 java.lang.NoClassDefFoundError: Could not initialize class sun.security.ec.SunEC 的解决办法
最近从 Op 那里报来一个问题,说是SFTP上传文件不成功.拿到的 Exception 如下: Caused by: java.lang.NoClassDefFoundError: Could not ...