jQuery on()方法是官方推荐的绑定事件的一个方法。使用 on() 方法可以给将来动态创建的动态元素绑定指定的事件,例如append等。
之前使用 on 的时候一直是
$("").on('click','function(){
}') 之后发现有些时候一直无法绑定(比如元素动态生成时),查看文档后发现正确用法应该是
$(document).on("change","#pageSize_out",function(){
if($("#page_out").val()!=0){
$("#pageSize").val($(this).val());
list();
}
}) 同时,注意
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。
替换bind()
当第二个参数’selector’为null时,on()和bind()其实在用法上基本上没有任何区别了,所以我们可以认为on()只是比bind()多了一个可选的’selector’参数,所以on()可以非常方便的换掉bind()
替换live()
在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() {










