jQuery event binding functions: on () and bind () difference

jQuery 1.7 version from the start, providing on () and off () to bind and to cancel the event handler. on () and bind () method with the same two areas have different areas.

bind(type,[data],fn);

on(type,[selector],[data],fn);

    The difference lies in whether to support the selector value of this parameter. If you are using on time, do not set the selector, so on and bind there is no difference.

e<div id="parent">
       <input type = "button" value="a" id=“a”>
       <input type = "button" value="b" id=“b”>
</div>

  The above code, if we use the bind () bind the click event handler on the parent, or b click on a button, the event handler will be executed. If we want to click on a time trigger, click the trigger b is not the time, you can use on, the code is as follows:

$("#parent").on("click","#a",function(){
    alert($(this).attr("id"));
})

bind methods can not achieve this function.

Another point to note: on binding event handlers, as for the future of the new elements can, and delegate the same effect, and bind is not.

 

delegate usage and on () the same, but the parameters of a different order:

delegate([selector],type,[data],fn);

There is also a live () method, in jQuery1.9 been removed, you can use on () to replace the role it was not recommended to use this method.

 

Published an original article · won praise 2 · views 10000 +

Guess you like

Origin blog.csdn.net/u011927449/article/details/104030858