jquery从1.7+版本开始就提供了on()和bind()的事件绑定的方法,对于bind和unbind的介绍可参考这篇文章
$(selector).bind(eventtype,data,function)$(selector).on(eventtype,childselect,data,function)
不难开出on方法多了一个childselect,由于js中事件委托(事件冒泡)的特性,如果在父元素上绑定一个事件,当子元素发生这个事件的时候,负元素也会被触发。这里如果不使用select,那么on和bind没啥区别了。例如:
<div id="parent"> <input type="button" value="a" id="a"/> <input type="button" value="b" id="b"/> </div> 这段代码,如果我们使用bind在id为parentd的div 上绑定click事件,当点击子元素任意一个input标签时,都会执行事件click,若要点击id为a的input标签触发click事件,点击id为b的input标签不触发click事件就照这样书写: $("#parent").on("click","#a",function(){ alert($(this).attr("id")); }); 显而易见,on函数的参数#a就是为了在事件冒泡的时候,让父元素过滤掉子元素上发生的事件。另外,on绑定事件处理函数,对于未来新增元素(比如由脚本创建的新元素)一样可以,和delegate一样,而bind不行
$(selector).delegate(childSelector,event,data,function)