For event binding methods in jQuery, there are mainly on(), bind(), delegate(), live(), etc. I have used it before, and I know there are several methods, but I don't know the difference between these event binding methods. The most commonly used method is the on method, and I plan to sort it out today.
bind method
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>bind事件綁定</title> </head> <body> <div> <button id="btn">添加新的p元素</button> <p>第一個p元素</p> <p>第二個p元素</p> <p>第三個p元素</p> <p>第四個p元素</p> <p>第五個p元素</p> </div> <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script> <script type="text/javascript"> //點(diǎn)擊添加一個新的元素 $("#btn").click(function(){ $("div").append("<p>這是一個新的p元素</p>"); }); //給所有的p元素綁定點(diǎn)擊事件 $("div p").click(function(){ alert($(this).text()); }); </script> </body> </html>
Disadvantages of this way of binding events:
When there are too many p elements on the page, there will be a large number of event handlers on the page, resulting in poor page performance;
There is no click event for dynamically generated new elements.
For these two shortcomings, we can solve them through the delegate method that will be introduced below.
delegate method
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>jQuery事件綁定</title> </head> <body> <div> <button id="btn">添加新的p元素</button> <p>第一個p元素</p> <p>第二個p元素</p> <p>第三個p元素</p> <p>第四個p元素</p> <p>第五個p元素</p> </div> <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script> <script type="text/javascript"> $("#btn").click(function(){ $("div").append("<p>這是一個新的p元素</p>"); }); $("div").delegate("p", "click", function () { alert($(this).text()); }); </script> </body> </html>
In the above example, we use the principle of event delegation and use delegate to bind events.
Do not bind events directly to the p element, but bind events to its parent element (or ancestor element). When you click on any element within the div, the event will pop up layer by layer from the event target (target element) bubble until it reaches the element to which you have bound the event, such as the div element in this example. During the bubbling process, if the event's currentTarget matches the selector, the code will be executed.
This solves the above two problems of using the bind() method. There is no need to bind events to p elements one by one, which effectively reduces the number of event handlers on the page. It can also bind events to dynamically added p elements. determined event.
This method is also flawed: it is easier to bind in this way, but problems may also occur when calling. If the event target is very deep in the DOM tree, bubbling up layer by layer to find elements that match the selector will affect performance.
bind and delegate source code implementation
bind: function( types, data, fn ) { return this.on( types, null, data, fn ); }, unbind: function( types, fn ) { return this.off( types, null, fn ); }, delegate: function( selector, types, data, fn ) { return this.on( types, selector, data, fn ); }, undelegate: function( selector, types, fn ) { // ( namespace ) or ( selector, types [, fn] ) return arguments.length === 1 ? this.off( selector, "**" ) : this.off( types, selector || "**", fn ); }
It can be seen from the source code that both bind() and delegate() are actually implemented through the on() method, but the parameters are different.
on method
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>jQuery事件綁定</title> </head> <body> <div> <button id="btn">添加新的p元素</button> <p>第一個p元素</p> <p>第二個p元素</p> <p>第三個p元素</p> <p>第四個p元素</p> <p>第五個p元素</p> </div> <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script> <script type="text/javascript"> //給每一個p元素綁定點(diǎn)擊事件 $("div").on("click","p",function(){ alert($(this).text()); }); </script> </body> </html>
Remove event
For bind(), delegate() and on() binding methods, the methods to remove events are:
$( "div p" ).unbind( "click", handler ); $( "div" ).undelegate( "p", "click", handler ); $( "div" ).off( "click", "p", handler );
Summary
?When there are many elements matched by the selector, do not use bind() for iterative binding;
?When using the id selector, you can use bind();
?When you need to bind dynamically added elements, use delegate() Or on();
?Use delegate() and on() methods, and the DOM tree should not be too deep;
?Use on() as much as possible.

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Detailed explanation of jQuery reference method: Quick start guide jQuery is a popular JavaScript library that is widely used in website development. It simplifies JavaScript programming and provides developers with rich functions and features. This article will introduce jQuery's reference method in detail and provide specific code examples to help readers get started quickly. Introducing jQuery First, we need to introduce the jQuery library into the HTML file. It can be introduced through a CDN link or downloaded

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: <

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on ??the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

jQuery is a popular JavaScript library that is widely used to handle DOM manipulation and event handling in web pages. In jQuery, the eq() method is used to select elements at a specified index position. The specific usage and application scenarios are as follows. In jQuery, the eq() method selects the element at a specified index position. Index positions start counting from 0, i.e. the index of the first element is 0, the index of the second element is 1, and so on. The syntax of the eq() method is as follows: $("s

jQuery is a popular JavaScript library widely used in web development. During web development, it is often necessary to dynamically add new rows to tables through JavaScript. This article will introduce how to use jQuery to add new rows to a table, and provide specific code examples. First, we need to introduce the jQuery library into the HTML page. The jQuery library can be introduced in the tag through the following code:

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute
