This article mainly introduces the usage of the on() method in jQuery. The example analyzes the function and definition of the on() method and the use of binding one or more event processing functions on matching elements. For tips, friends who need them can refer to
This article explains the usage of the on() method in jQuery with examples. Share it with everyone for your reference. The specific analysis is as follows:
This method can bind one or more event handling functions to the matching element.
Use the off() method to delete the event bound by the on() method.
Grammar structure one:
The code is as follows:
$(selector).on(events,[selector], [data],fn)
Parameter list:
Example code:
Example 1:
The code is as follows:
<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="http://www.jb51.net/" /> <title>脚本之家</title> <style type="text/css"> p{ width:60px; height:60px; border:1px solid green; font-size:12px; } </style> <script type="text/ javascript " src="mytest/jQuery/jquery-1.8.3.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("p").on("click",function(){ $(this).text("脚本之家欢迎您"); }) }) </script> </head> <body> <p>原来内容</p> </body> </html>
The above code binds a click event to p. When p is clicked Ability to set new text content for p.
Example 2:
The code is as follows:
<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="http://www.jb51.net/" /> <title>脚本之家</title> <style type="text/css"> p{ width:60px; height:60px; border:1px solid green; font-size:12px; } </style> <script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("p").on("click mousemove",function(){ $(this).text("脚本之家欢迎您"); }) }) </script> </head> <body> <p>原来内容</p> </body> </html>
The above code binds two events to p. Whether you click p or move the mouse in p, a new event will be set for p. text content.
Example 3:
The code is as follows:
<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="http://www.jb51.net/" /> <title>脚本之家</title> <style type="text/css"> p{ width:60px; height:60px; border:1px solid green; font-size:12px; } </style> <script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script> <script type="text/javascript"> $(document).ready(function(){ var newtext="这是新文本" $("p").on("click",{"mytext":newtext},function(e){ $(this).text(e.data.mytext); }) }) </script> </head> <body> <p>原来内容</p> </body> </html>
The above code uses the data parameter to pass data to the bound event processing function.
Example 4:
The code is as follows:
<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="http://www.jb51.net/" /> <title>脚本之家</title> <style type="text/css"> .parent{ width:160px; height:160px; border:1px solid green; font-size:12px; } .children{ width:100px; height:100px; border:1px solid red; } span{background-color:green;} </style> <script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script> <script type="text/javascript"> $(document).ready(function(){ var newtext="这是新文本" $(".parent").on("click",".children",{"mytext":newtext},function(e){ $(this).text(e.data.mytext); }) }) </script> </head> <body> <p class="parent"> <p class="children"><span>原来内容</span></p> </p> <span>大家好</span> </body> </html>
The above code uses the selector string to filter which of the child elements of the matching element can respond to the bound event. In the above code, the element with the class name children and its child elements can call bound events.
Grammar structure two:
The code is as follows:
$(selector).on(object,[selector],[data])
parameter list:
实例代码:
代码如下:
<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="http://www.jb51.net/" /> <title>脚本之家</title> <style type="text/css"> p{ width:160px; height:160px; border:1px solid green; font-size:12px; } </style> <script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script> <script type="text/javascript"> $(document).ready(function(){ var newtext="这是新文本" $("p").on({click:function(e){ $(this).text(e.data.mytext); }},{"mytext":newtext}) }) </script> </head> <body> <p>原来内容</p> </body> </html>
以上代码中,事件类型和事件处理函数以对象的形式绑定的。
The above is the detailed content of Analyze the role and usage of the on() function in jQuery. For more information, please follow other related articles on the PHP Chinese website!