以前使用 on 的时候一直是web
$("").on('click','function(){ }') less
以后发现有些时候一直没法绑定(好比元素动态生成时),查看文档后发现正确用法应该是函数
$(document).on("change","#pageSize_out",function(){ 性能
if($("#page_out").val()!=0){ this
$("#pageSize").val($(this).val()); spa
list(); 代理
} code
}) orm
同时,注意接口
As this answers receives a lot of attention, here are two supplementary advises :
1) When it's possible, try to bind the event listener to the most precise element, to avoid useless event handling.
That is, if you're adding an element of class b
to an existing element of id a
, then don't use
$(document.body).on('click', '#a .b', function(){
but use
$('#a').on('click', '.b', function(){
2) Be careful, when you add an element with an id, to ensure you're not adding it twice. Not only is it "illegal" in HTML to have two elements with the same id but it breaks a lot of things. For example a selector "#c"
would retrieve only one element with this id.
on(events,[selector],[data],fn)
events:一个或多个用空格分隔的事件类型和可选的命名空间,如"click"或"keydown.myPlugin" 。
selector:一个选择器字符串用于过滤器的触发事件的选择器元素的后代。若是选择器为null或省略,当它到达选定的元素,事件老是触发。
data:当一个事件被触发时要传递event.data给事件处理函数。
fn:该事件被触发时执行的函数。 false 值也能够作一个函数的简写,返回false。
当第二个参数'selector'为null时,on()和bind()其实在用法上基本上没有任何区别了,因此咱们能够认为on()只是比bind()多了一个可选的'selector'参数,因此on()能够很是方便的换掉bind()
在1.4以前相信你们很是喜欢使用live(),由于它能够把事件绑定到当前以及之后添加的元素上面,固然在1.4以后delegate()也能够作相似的事情了。live()的原理很简单,它是经过document进行事件委派的,所以咱们也可使用on()经过将事件绑定到document来达到live()同样的效果。
live()写法
$('#list li').live('click', '#list li', function() {
//function code here.
});
on()写法
$(document).on('click', '#list li', function() {
//function code here.
});
这里的关键就是第二个参数'selector'在起做用了。它是一个过滤器的做用,只有被选中元素的后代元素才会触发事件。
替换delegate()
delegate()是1.4引入的,目的是经过祖先元素来代理委派后代元素的事件绑定问题,某种程度上和live()优势类似。只不过live()是经过document元素委派,而delegate则能够是任意的祖先节点。使用on()实现代理的写法和delegate()基本一致。
delegate()的写法
$('#list').delegate('li', 'click', function() {
//function code here.
});
on()写法
$('#list').on('click', 'li', function() {
//function code here.
});
貌似第一个和第二个参数的顺序颠倒了一下,别的基本同样。
总结jQuery推出on()的目的有2个,一是为了统一接口,二是为了提升性能,因此从如今开始用on()替换bind(), live(), delegate吧。尤为是不要再用live()了,由于它已经处于不推荐使用列表了,随时会被干掉。若是只绑定一次事件,那接着用one()吧,这个没有变化。