The javascript on method is used to add one or more event handlers on the selected element and child elements. Its syntax is "$(selector).on(event,childSelector,data,function)".
The operating environment of this article: windows7 system, jquery version 1.10.2, DELL G3 computer.
on method definition and usage
on() method adds one or more event handlers on the selected element and sub-elements.
Since jQuery version 1.7, the on() method is the new replacement for the bind(), live(), and delegate() methods. This method brings a lot of convenience to the API and is recommended because it simplifies the jQuery code base.
Note: Event handlers added using the on() method apply to current and future elements (such as new elements created by scripts).
Tip: If you need to remove the event handler, use the off() method.
Tip: If you need to add an event that only runs once and then remove it, use the one() method.
Syntax
$(selector).on(event,childSelector,data,function)
Parameter Description
event Required. Specifies one or more events or namespaces to be added from the selected element.
Multiple event values are separated by spaces or can be an array. Must be a valid event.
childSelector Optional. Specifies that event handlers can only be added to specified child elements (and not the selector itself, such as the deprecated delegate() method).
data Optional. Specifies additional data to be passed to the function.
function Optional. Specifies a function to run when an event occurs.
Example:
<!DOCTYPE html> <html> <head> <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("p").on("click",function(){ alert("段落被点击了。"); }); }); </script> </head> <body> <p>点击这个段落。</p> </body> </html>
Effect:
The above is the detailed content of How to use javascript on method. For more information, please follow other related articles on the PHP Chinese website!