With the update of the jQuery version, the Bind(), live(), and delegate() events of previous versions can be directly replaced by on. This note only takes the click event as an example, and mainly examines the usage of the on event: This article It mainly introduces the matters that need to be paid attention to when using on to bind events in jQuery, which has a good reference value. Let's take a look with the editor below, I hope it can help everyone.
1. Use on to monitor the DOM added before the click event, which can be triggered normally:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <style type="text/css"> #test { height: 100px; font-size: 50px; color: yellow; line-height: 100px; background-color: red; text-align: center; } </style> <script src="http://lib.sinaapp.com/js/jquery/2.0.2/jquery-2.0.2.min.js"></script> <script> $( function(){ var op = $('<p id="test">点我</p>'); $(document.body).append(op); $("#test").on('click', function(){ alert('这是事件前添加的DOM,可以正常触发点击事件'); }); } ); </script> </head> <body> <p id="wrap"></p> </body> </html>
2. Use The DOM added after on monitors the click event cannot be triggered normally:
<!-- 一、正常的DOM: --> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <style type="text/css"> #test { height: 100px; font-size: 50px; color: yellow; line-height: 100px; background-color: red; text-align: center; } </style> <script src="http://lib.sinaapp.com/js/jquery/2.0.2/jquery-2.0.2.min.js"></script> <script> $( function(){ $("#test").on('click', function(){ alert('这是事件后添加的DOM,无法正常触发点击事件'); }); var op = $('<p id="test">点我</p>'); $(document.body).append(op); } ); </script> </head> <body> <p id="wrap"></p> </body> </html>
3. Solution: Use event delegation. It should be noted that the delegated object must already exist in the DOM or be added dynamically before the event. At this time, the event can be triggered normally after using on to listen. Otherwise, as in: Step 4:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <style type="text/css"> #test { height: 100px; font-size: 50px; color: yellow; line-height: 100px; background-color: red; text-align: center; } </style> <script src="http://lib.sinaapp.com/js/jquery/2.0.2/jquery-2.0.2.min.js"></script> <script> $( function(){ var oWrap = $('<p id="wrap"></p>'); $(document.body).append(oWrap); $("#wrap").on('click', $('#test'), function(){ alert('委托对象在事件前添加,可以正常触发点击事件'); }); var op = $('<p id="test">点我</p>'); $("#wrap").append(op); } ); </script> </head> <body> </body> </html>
4. The delegate object is added after the event and the click event cannot be triggered
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <style type="text/css"> #test { height: 100px; font-size: 50px; color: yellow; line-height: 100px; background-color: red; text-align: center; } </style> <script src="http://lib.sinaapp.com/js/jquery/2.0.2/jquery-2.0.2.min.js"></script> <script> $( function(){ $("#wrap").on('click', $('#test'), function(){ alert('委托对象在事件后添加,无法正常触发点击事件'); }); var oWrap = $('<p id="wrap"></p>'); $(document.body).append(oWrap); var op = $('<p id="test">点我</p>'); $("#wrap").append(op); } ); </script> </head> <body> </body> </html>
5. Description: At work, the most commonly used on events are event binding and event delegation. At least I encounter them more often. The main usage of live event on is that the previous jQuery object becomes a document, such as: $(document).on(), Therefore no other examples will be given.
Related recommendations:
Solution to the invalid id binding event in jquery appaend element
Detailed explanation of the problem of JQuery binding events in a loop
Javascript uses event attributes to bind event functions. Detailed explanation of usage
The above is the detailed content of What should you pay attention to when using on to bind events in jQuery?. For more information, please follow other related articles on the PHP Chinese website!