


Detailed explanation of the steps to dynamically add click events in jquery
This time I will bring you a detailed explanation of the steps for dynamically adding click events to jquery. What are the precautions for jquery to dynamically add click events? Here is a practical case. Let’s take a look. .
When we try to bind some events to DOM elements, we usually use the following four methods bind(),on(),live( ), delegate()
The first two methods should be used more often. The following is my understanding of the four methods:
Bind(): .bind() is the most direct binding method, which will bind the specified function and Events are transferred to the DOM. This method solves the browser compatibility problem in Event processing, but this method still has some problems. Code:
$( "#members li a" ).bind( "click", function( e ) {} ); $( "#members li a" ).click( function( e ) {} );
The above two lines of code accomplish the same task, which is to add the event handler to all matching a elements. There are some efficiency issues here. On the one hand, we add click events to all a tags. This process is expensive; on the other hand, it is also a waste during execution, because they all do The same thing is executed many times (we can hook them to their parent elements, distinguish each of them by bubbling, and then execute the event handle).
Advantages
This method provides a compatibility solution for event handling between various browsers
Very convenient binding events to elements
.click(), .hover()...These very convenient time bindings are all bind A simplified processing method
is very good for elements selected by ID. Not only can it be hooked quickly (the page can only have one ID), but when the event When it occurs, the handler can be executed immediately (equivalent to the later live, delegate) implementation method
Disadvantages
It will bind events to all filtered elements
It will not bind events to those elements that are dynamically added after it completes execution
When there are many filtered elements, efficiency problems may occur.
Bind() can only be performed when the page is loaded, so efficiency problems may occur.
.live()
.live() method uses the concept of event delegation to handle event binding. It is the same as using .bind() to bind events. The .live() method will bind the corresponding event to the root element of the element you selected, which is the document element. Then all events that bubble up can be handled by this same handler. Its processing mechanism is like this. Once the event bubbles to the document, jQuery will look for the selector/event metadata and then decide which handler should be called. However, it seems to have been deleted in the latest jquery version.
$( "#members li a" ).live( "click", function( e ) {} );
Advantages:
#There is only one event binding here, which is bound to the document instead of .bind(). All elements are bound one by one
Those dynamically added elemtns can still trigger events that were bound earlier, because the actual binding of the event is on the document
You can bind the required events before the document is ready
Disadvantages:
- # #It has been deprecated since 1.7, so you have to start phasing it out too.
- Chaining is not supported correctly
- It is useless when using event.stopPropagation() because it has to reach the document
- Because all selectors/events are bound to documents,
so when we use the matchSelector method to select which event is called, it will be very slow
- When the element where the event occurs is very deep in your DOM tree, there will be performance problems
.Delegate()
.delegate()有点像.live(),不同于.live()的地方在于,它不会把所有的event全部绑定到document,而是由你决定把它放在哪儿。而和.live()相同的地方在于都是用event delegation.推荐使用.delegate() 代替.live()
$( "#members" ).delegate( "li a", "click", function( e ) {} );
优点:
你可以选择你把这个事件放到那个元素上了 chaining被正确的支持了
jQuery仍然需要迭代查找所有的selector/eventdata来决定那个子元素来匹配,但是因为你可以决定放在那个根元素上,所以可以有效的减小你所要查找的元素。
可以用在动态添加的元素上
缺点:
需要查找那个那个元素上发生了那个事件了,尽管比document少很多了,不过,你还是得浪费时间来查找。
.On()
其实.bind(), .live(), .delegate()都是通过.on()来实现的,.unbind(), .die(), .undelegate(),也是一样的都是通过.off()来实现的,这是1.8.2的源码:
$( "#members li a" ).on( "click", function( e ) {} ); $( "#members li a" ).bind( "click", function( e ) {} ); // Live $( document ).on( "click", "#members li a", function( e ) {} ); $( "#members li a" ).live( "click", function( e ) {} ); // Delegate $( "#members" ).on( "click", "li a", function( e ) {} ); $( "#members" ).delegate( "li a", "click", function( e ) {} );
优点:
提供了一种统一绑定事件的方法
仍然提供了.delegate()的优点,当然如果需要你也可以直接用.bind()
缺点:
也许会对你产生一些困扰,因为它隐藏了一前面我们所介绍的三种方法的细节。
结论:
用.bind()的代价是非常大的,它会把相同的一个事件处理程序hook到所有匹配的DOM元素上
不要再用.live()了,它已经不再被推荐了,而且还有许多问题
.delegate()会提供很好的方法来提高效率,同时我们可以添加一事件处理方法到动态添加的元素上。
我们可以用.on()来代替上述的3种方法
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
webkit-font-smoothing字体抗锯齿渲染使用案例详解
The above is the detailed content of Detailed explanation of the steps to dynamically add click events in jquery. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

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

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

Undress AI Tool
Undress images for free

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)

Hot Topics

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: <

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

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:

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

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

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:
