Home  >  Article  >  Web Front-end  >  What should you pay attention to when using on to bind events in jQuery?

What should you pay attention to when using on to bind events in jQuery?

小云云
小云云Original
2018-01-22 11:21:081287browse

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 = $(&#39;<p id="test">点我</p>&#39;);
 $(document.body).append(op);
 $("#test").on(&#39;click&#39;, function(){
 alert(&#39;这是事件前添加的DOM,可以正常触发点击事件&#39;);
 });
 } );
</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(&#39;click&#39;, function(){
 alert(&#39;这是事件后添加的DOM,无法正常触发点击事件&#39;);
 });
 var op = $(&#39;<p id="test">点我</p>&#39;);
 $(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 = $(&#39;<p id="wrap"></p>&#39;);
 $(document.body).append(oWrap);
 $("#wrap").on(&#39;click&#39;, $(&#39;#test&#39;), function(){
 alert(&#39;委托对象在事件前添加,可以正常触发点击事件&#39;);
 });
 var op = $(&#39;<p id="test">点我</p>&#39;);
 $("#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(&#39;click&#39;, $(&#39;#test&#39;), function(){
 alert(&#39;委托对象在事件后添加,无法正常触发点击事件&#39;);
 });
 var oWrap = $(&#39;<p id="wrap"></p>&#39;);
 $(document.body).append(oWrap);
 var op = $(&#39;<p id="test">点我</p>&#39;);
 $("#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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn