jQuery1.3:Live Events
发表评论»jQuery 1.3中新增了两个“实时”事件处理方法——live,die,用于绑定和删除事件处理函数。这两个方法与之前的bind和unbind存在明显的区别。
live( type, fn )
Added in jQuery 1.3: Binds a handler to an event (like click) for all current – and future – matched element. Can also bind custom events.
bind( type, [data], fn )
Binds a handler to one or more events (like click) for each matched element. Can also bind custom events.
bind只能对当前的匹配对象绑定事件处理函数,而live不但能够对当前的匹配对象绑定事件处理函数,还能够对将来的匹配对象绑定事件处理函数。
例如:
$("button").bind("click",function(){
$(this).after("<button>click me</button>");
}
);
使用bind无法对后面创建的对象进行“click”事件处理函数的绑定。
而使用live则完全可以对将来的匹配对象进行事件处理函数的绑定:
$("div").live("click",function(){
$(this).after("<div>div:click me</div>");
}
)