jquery .live() .delegate() .bind() .click()区别
什么是.live()?
除了让你对Dom元素现在和将来绑定事件之外,.live() 方法和.bind()方法很像。你可以用.live()方法对没有存在的Dom节点绑定事件。考虑下面的情况。
当用户要离开你的站点时,点击任何连接,你可以警告他:
|
1
2
3
4
5
6
|
$(document).ready( function() { $('a').click( function() { alert("You are now leaving this site"); return true; });}); |
注意:.click()相对于.bind()方法仅仅是一个方便的方法,所以下面的方法和上面是等价的:
|
1
2
3
4
5
6
|
$(document).ready(function(){ $('a').bind('click',function(){ alert('You are leaving this site'); return true; })}) |
但是现在,你通过javascript在页面添加另一个链接。
|
1
|
$('body').append('<div><a href="...">Check it out !</a></div>'); |
现在,当用户点击链接时,你的方法不会触发。因为当你对页面所有的<a> 节点,绑定事件的时候,那个链接并不存在。所以我们可以简单的使用.live()取代.bind()。
|
1
2
3
4
5
6
|
$(document).ready( function() { $('a').live( 'click', function() { alert("You are now leaving this site"); return true; });}); |
现在,如果你添加一个链接到页面,这个绑定的方法就会工作。
.live()怎样工作?
.live()方法并不是绑定到你指定的那个元素上,它实际上是绑定到Dom树的根节点(在我们的例子里,指的是$(document)),把你选定的元素作为参数传递过去。
所以,当你点击一个元素的时候,点击事件会冒泡到根节点。.live()方法执行时,会检查目标元素是否匹配,事件是否是指定的事件。如果都返回true,才会执行事件。
任何.live() 都可以被.die()
如果你了解.bind(),你肯定知道.unbind()。.die()对于.live()也是相同的作用。
当去掉上面例子的事件(不想提醒用户),我们可以简单的:
|
1
|
$('a').die(); |
更特别的,如果我们有其他的事件想保留(像hover或者其他的),我们可以只去掉click事件,
|
1
|
$('a').die('click'); |
更特别的是,我们可以去掉特定事件的特定方法。
|
1
2
3
4
5
6
7
8
9
10
11
12
|
specialAlert = function() { alert("You are now leaving this site"); return true;}$(document).ready( function() { $('a').live( 'click', specialAlert ); $('a').live( 'click', someOtherFunction );});// then somewhere else, we could unbind only the first binding$('a').die( 'click', specialAlert ); |
.die()的缺点。
当使用.die()去掉.live()时,你只能用和.live()方法相同的目标元素。例如,下面是不行的:
|
1
2
3
4
5
6
7
8
9
10
|
$(document).ready( function() { $('a').live( 'click', function() { alert("You are now leaving this site"); return true; });});// it would be nice if we could then choose specific elements// to unbind, but this will do nothing$('a.no-alert').die(); |
.die()是绑定到由.live()创建的根节点,然后匹配目标元素,去掉他们。但是在上面的例子中,.live()绑定的是$('a.no-alert'),所以jQuery找不到任何东西去取消。
更严重的是:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
$(document).ready( function() { $('a,form').live( 'click', function() { alert("You are going to a different page"); return true; });});// NEITHER of these will work$('a').die();$('form').die();// ONLY this will work$('a,form').die(); |
jquery .live() .delegate() .bind() .click()区别的更多相关文章
- jQuery live与bind的区别
平时在使用jQuery进行AJAX操作的时候,新生成的元素事件会失效,有时候不得不重新绑定一下事件,但是这样做很麻烦.例如评论分页后对评论内容的JS验证会失效等.在jQuery1.3之前有一个插件会解 ...
- jQuery中的.bind()、.live()和.delegate()之间区别分析
jQuery中的.bind()..live()和.delegate()之间区别分析,学习jquery的朋友可以参考下. DOM树 首先,可视化一个HMTL文档的DOM树是很有帮助的.一个简单的 ...
- jQuery中的bind() live() delegate()之间区别分析
jQuery中的bind() live() delegate()之间区别分析 首先,你得要了解我们的事件冒泡(事件传播)的概念,我先看一张图 1.bind方式 $('a').bind('click', ...
- jQuery绑定事件方法及区别(bind,click,on,live,one)
第一种方式: ? 1 2 3 4 5 $(document).ready(function(){ $("#clickme").click(function(){ alert(& ...
- jQuery 请指出'.bind()','.live()'和'.delegate()'的区别
http://kb.cnblogs.com/page/94469/网上好多类似的 简言之bind可以绑定页面上已有的,live和delegate可以绑定到还未存在于页面中的元素之上.delegate又 ...
- jQuery事件函数bind,live,delegate的区别
DOM树 首先,可视化一个HMTL文档的DOM树是很有帮助的.一个简单的HTML页面看起来就像是这个样子: 事件冒泡(又称事件传播) 当我们点击一个链接时,其触发了链接元素的单击事件,该事件则引发任何 ...
- jQuery事件:bind、delegate、on的区别
最近在AngularJS的开发中,遇到一个神奇的事情:我们用到livebox来预览评论列表中的图片, 然而评论列表是由Angular Resource动态载入的.不可思议的是,点击这些动态载入的图片仍 ...
- jquery中bind,live,delegate,on的区别
这几种方法都是绑定事件用到的,但是他们之间有些差别 bind(type,[data],fn) 为每个匹配元素的特定事件绑定事件处理函数 例如: <ul> <a href=" ...
- 【转】jQuery中.bind() .live() .delegate() .on()的区别
bind(type,[data],fn) 为每个匹配元素的特定事件绑定事件处理函数 $("a").bind("click",function(){alert(& ...
随机推荐
- Java Callable接口与Future接口的两种使用方式
Java Callable.Future的两种使用方式Callable+Futurepublic class Test { public static void main(String[] args) ...
- HTTP Error 502.5 – Process Failure
https://www.cnblogs.com/lookerblue/p/7101641.html http://www.cnblogs.com/lookerblue/p/7102040.html h ...
- Mockito 的使用
转自:Mockito 中文文档 ( 2.0.26 beta ) 转自:手把手教你 Mockito 的使用 参数匹配器 Argument Matcher(参数匹配器) Mockito通过equals() ...
- Ubuntu sh命令无法正确执行 (修改默认sh为bash)
新安装的ubuntu,执行shell命令,都怀疑自己的shell水平了. 原来, ubuntu默认的是dash, 需要手动执行命令将dash改为bash. 命令: sudo dpkg-reconfig ...
- C++/Php/Python 语言执行shell命令
编程中经常需要在程序中使用shell命令来简化程序,这里记录一下. 1. C++ 执行shell命令 #include <iostream> #include <string> ...
- ubuntu下nodejs开发环境搭建
1.安装nodejs sudo apt install -y nodejs 2.更新npm到最新版本 sudo npm i -g npm 3.npm配置为淘宝镜像 sudo npm config se ...
- 2018.08.28 ali 梯度下降法实现最小二乘
- 要理解梯度下降和牛顿迭代法的区别 #include<stdio.h> // 1. 线性多维函数原型是 y = f(x1,x2,x3) = a * x1 + b * x2 + c * x ...
- windows 使用 xxfpm 解决 php-cgi 进程自动关闭
windows 下 php-cgi 进程处理一定数量的访问后,就会自动关闭,由于没办法直接让 php-cgi 进程支持更多的访问数量,所以只能启动多个进程来满足需求. xxfpm 是一个可执行程序,它 ...
- Linux Kernel 2:用户空间的初始化
上篇我们知道,kernel初始化后将启动init进程,那么这个进程将干些什么呢?除此之外,kernel还需要做些什么事情呢?(想想文件系统.根存储设备是在什么时候初始化的呢?) 先从文件系统初始化说起 ...
- OpenGL ES 3.0 and libGLESv2
note that libGLESv2 is the recommended Khronos naming convention for the OpenGL ES 3.0 library. This ...