jquery 中 事件绑定函数 bind() ,live(),delegate()以及on() 的区别
2016-10-26 22:00
831 查看
jquery中事件绑定的方法有好几种,下面是我对这些方法的理解
1.bind(),我们最常使用的方法之一,它会给每个匹配的元素绑定 事件以及添加时间处理函数
$(” div>ul li”).bind(“click”,function(){
alert(” this is a test”);
})
bind 给ul 中的每一li元素绑定点击事件和时间处理程序
2.live() 利用时间委托机制,它会将事件绑定到匹配元素的根元素(document)上
live()会强制默认根元素为document
3.delegate() 与live() 类似 它会将事件绑定到指定的根元素上 并不会强制限定根元素
4 on() on方法是以上三种方式的总和`
//bind
( “#foo” ).bind( “click”, function( e ) {} );( “#foo” ).on( “click”, function( e ) {} );
// delegate
( “#root” ).delegate( “a”, “click”, function( e ) {} );( “#root” ).on( “click”, “a”, function( e ) {} );
“`
`
1.bind(),我们最常使用的方法之一,它会给每个匹配的元素绑定 事件以及添加时间处理函数
<div> <ul> <li></li> <li></li> <li></li> </ul> </div>
$(” div>ul li”).bind(“click”,function(){
alert(” this is a test”);
})
bind 给ul 中的每一li元素绑定点击事件和时间处理程序
2.live() 利用时间委托机制,它会将事件绑定到匹配元素的根元素(document)上
live()会强制默认根元素为document
3.delegate() 与live() 类似 它会将事件绑定到指定的根元素上 并不会强制限定根元素
div id="root"> <a>Alice</a> <a>Bob</a> </div> <script> $('#root').delegate('a', 'click', function(){ console.log('clicked'); }); </script>
4 on() on方法是以上三种方式的总和`
//bind
( “#foo” ).bind( “click”, function( e ) {} );( “#foo” ).on( “click”, function( e ) {} );
// delegate
( “#root” ).delegate( “a”, “click”, function( e ) {} );( “#root” ).on( “click”, “a”, function( e ) {} );
“`
`
相关文章推荐
- 原生JS事件绑定方法以及jQuery绑定事件方法bind、live、on、delegate的区别
- [TOP]原生JS事件绑定方法以及jQuery绑定事件方法bind、live、on、delegate的区别
- jQuery中bind,live,delegate,on绑定事件的方式与区别
- jQuery四种事件绑定方式.bind(),.live(),.delegate(),on()的区别
- jQuery四种事件绑定方式.bind(),.live(),.delegate(),on()的区别
- Jquery中的bind(),live(),delegate(),on()绑定事件方式的区别
- jQuery四种事件绑定方式.bind(),.live(),.delegate(),on()的区别
- jQuery中的绑定事件_on,bind,live,delegate的区别
- jQuery事件绑定方法bind、 live、delegate和on的区别
- 【jquery】jquery绑定事件的方式 bind() delegate() live()以及on()
- jQuery事件绑定方法bind、 live、delegate和on的区别
- jQuery四种事件绑定方式.bind(),.live(),.delegate(),on()的区别
- jQuery中的事件绑定函数.bind()、.live()、on()和.delegate()
- jQuery事件绑定方法bind、 live、delegate和on的区别
- 【jquery】jquery绑定事件的方式 bind() delegate() live()以及on()
- 【转】jQuery中的bind(),live(),delegate(),on()事件绑定方式的区别
- jQuery事件绑定bind、live、delegate和on的区别
- jQuery中bind(),live(),delegate(),on()绑定事件方法实例详解
- Jquery click/bind/live/delegate/on事件,阻止默认事件,以及插件的写法
- jQuery事件绑定on、off 和one,取代bind, live, delegate