jquery 使用on方法给元素绑定事件

on方法在1.7版本中开始出现的,现在已经优先考虑on,并不是bind方法了。

on( events [,selector] [,data] ,handler)

  event:为事件类型,可以有多个事件类型。

  selector:可选,过滤绑定在on方法上的后代元素。注:如果有selector,on方法是采用事件代理,这样可以提升代码性能。

  data:可选,当事件被触发时,它会传给event.data,从而可以加以利用

  handler:当事件被触发时,执行的方法。

$(document).on("blur","#padRadius",{name:"vichin"},function(event){
    alert(event.data.name)
});
$(document).on("blur", "#padRadius", function () {
           alert("hello word!");
        });

等同于bind的用法

$("#div1").on("click",function(){
    alert("Hello word");
  });

 $("#div1").click(function(){

});

这种写法在1.7之前是bind的简写,在1.7之后,就是on的简写

解绑:

$("#div1").off("click");
原文地址:https://www.cnblogs.com/vichin/p/7357762.html