on使用详解
on()是bind(),live(),delegate()的替代品,1.7及1.7以后使用on()
bind() 绑定元素
live() 为元素附加事件,匹配选择器的当前及未来的元素(比如由脚本创建的新元素)
delegate() 绑定被选元素的子元素
使用介绍:
$(selector).on(event,childSelector,data,function,map)
替代live
$("#a").live("click",function(){
});
$("#a").on("click", function(){
});
$("ul").delegate("li", "click", function(){
});
$("ul").on("click", "li", function(){
});
绑定多个事件
$("#a").on("mousemove mouseout", function(){
});多个事件分别绑定
$("p").on({
mouseover:function(){$("body").css("background-color","lightgray");},
mouseout:function(){$("body").css("background-color","lightblue");},
click:function(){$("body").css("background-color","yellow");}
});
mouseover:function(){$("body").css("background-color","lightgray");},
mouseout:function(){$("body").css("background-color","lightblue");},
click:function(){$("body").css("background-color","yellow");}
});
此外 用off()来移除on()创建的事件,替代unbind(), undelegate(), die()