Home Web Front-end JS Tutorial Jquery binding event (introduction to the difference between bind and live)_jquery

Jquery binding event (introduction to the difference between bind and live)_jquery

May 16, 2016 pm 05:24 PM
bind live

There are three ways to bind events in Jquery: Take the click event as an example

(1) target.click(function(){});

(2) target.bind("click" ,function(){});

(3) target.live("click",function(){});

The first method is easy to understand. In fact, it is the same as ordinary The usage of JS is similar, except that one on is missing

The second and third methods are all binding events, but they are very different. Let’s focus on explaining this, because if you use Jquery The framework is used quite a lot, so we should pay special attention to the difference between the two.

[The difference between bind and live]

The live method is actually a variant of the bind method. Its basic function is the same as the bind method. They both bind an element to an element. Events, but the bind method can only bind events to currently existing elements, and is invalid for newly generated elements using JS and other methods. The live method just makes up for this flaw of the bind method, and it can also be used for later generated elements. Bind the corresponding event. So how is this feature of the live method implemented? Let’s discuss its implementation principle below.

The reason why the live method can also bind corresponding events to later generated elements is attributed to "event delegation". The so-called "event delegation" means that events bound to ancestor elements can be Used on descendant elements. The processing mechanism of the live method is to bind the event to the root node of the DOM tree instead of directly binding it to an element. Give an example to illustrate:

Copy code The code is as follows:

$(".clickMe" ).live("click",fn);
$("body").append("
Steps to test the live method
");

When we click on this new element, the following steps will occur:

(1) Generate a click event and pass it to the div for processing

(2) Since no event is directly bound to the div, the event bubbles directly to the DOM tree

(3) The event continues to bubble until it reaches the root node of the DOM tree. By default, the root node is bound Set this click event

(4) Execute the click event bound by live

(5) Detect whether the object of the bound event exists, and determine whether it is necessary to continue to execute the bound event. Detecting event objects is achieved by detecting whether

$(event.target).closest('.clickMe') can find matching elements.

(6) Through the test of (5), if the object bound to the event exists, the bound event will be executed.

Since the live method will detect whether the object bound to the event exists only when an event occurs, the live method can implement later added elements and event binding. In contrast, bind will determine whether the element to which the event is bound exists during the binding phase of the event, and will only bind to the current element, not to the parent node.

According to the above analysis, the benefits of live are really great, so why should we use the bind method? The reason why jquery retains the bind method instead of using the live method to replace bind is because live cannot completely replace bind in some cases. The main differences are as follows:

(1) The bind method can bind any JavaScript event, while the live method only supports click, dblclick, keydown, keypress, keyup, mousedown, mousemove, mouseout in jQuery1.3 , mouseover, and mouseup. In jQuery 1.4.1, focus and blue

events are even supported (mapped to the more appropriate focusin and focusout that can bubble). In addition, in jQuery 1.4.1, hover (mapped to "mouseenter mouseleave") is also supported.

(2) live() does not fully support elements found through DOM traversal. Instead, you should always use the .live() method directly after a selector.

(3) When an element uses the live method to bind events, if you want to prevent the event from being delivered or bubbling, you must return false in the function. Simply calling stopPropagation() cannot prevent it. Event delivery or bubbling
Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Solution to PHP Fatal error: Call to undefined function ldap_bind() Solution to PHP Fatal error: Call to undefined function ldap_bind() Jun 22, 2023 pm 11:37 PM

When developing web applications using PHP, we often need to use LDAP authentication to protect application access. However, in some cases, when we try to use PHP's LDAP functionality to implement authentication, we may encounter the following error message: "PHPFatalerror:Calltoundefinedfunctionldap_bind()". This error message usually occurs when an application calls the ldap_bind() function

what is linux bind what is linux bind Mar 25, 2023 am 09:39 AM

Linux bind is a command used to display or set keyboard keys and their related functions. You can use the bind command to understand which key combinations and functions they have, or you can specify which key combinations to use; its usage syntax is "bind [-dlv][ -f <...>][-m <...>][-q <...>]".

How to fix 0x87dd0019 Xbox login error How to fix 0x87dd0019 Xbox login error Mar 22, 2024 pm 02:30 PM

This article will guide you to fix the 0x87dd0019 Xbox login error, which causes connection timeout issues when you try to connect to Xbox Live or log in to Xbox One. What is error code 0x87e00019 on Xbox? If you encounter error code 0x87e00019 when installing or updating games on your Xbox console, it means your Xbox hard drive may be low on storage space or nearly full. To solve this problem, you need to free up some storage space. At the same time, you should also check the status of the Xbox Live service, as this error may be due to Xbox server issues. How to fix 0x87dd0019 Xbox login error using these tips

What should I do if I can't log in to xbox live on win10? Win10 cannot log in to xbox live solution What should I do if I can't log in to xbox live on win10? Win10 cannot log in to xbox live solution Feb 15, 2024 am 11:51 AM

Xbox is Microsoft's own network service center. Many users find that their Win10 computers cannot log in to Xbox Live when playing. So what should they do? Let this site carefully introduce to users the solution to the problem of being unable to log in to xboxlive in win10. Solution to the problem of being unable to log in to xboxlive in Windows 10: 1. Open the run window with the "win+R" shortcut key, enter "services.msc", and press Enter to open it. 2. After entering the "Service" window interface, find "xboxlive Authentication Manager" on the right and double-click to open it.

Implementation and use of bind in JS Implementation and use of bind in JS Feb 24, 2024 pm 01:33 PM

Implementation and use of bind in JS In JavaScript, bind is a very useful function method. It can create a new function, while ensuring that when the function is called, it has a specific this value and can pass the specified parameters. The bind method is defined as follows: functionbind(fn,obj,...args){returnfunction(...args2){return

Does premiere mean live? Does premiere mean live? Apr 04, 2025 am 12:07 AM

"Premiere" and "live" have different meanings in video production: "premiere" refers to the first release or premiere, while "live" refers to the live broadcast in real time. 1. "Premiere" is the first display of pre-recorded content. 2. Setting up a premiere in Adobe PremierePro involves editing, editing, and rendering, and then scheduling the premiere time. 3. Use Python scripts to schedule video premieres. 4. Key steps include exporting settings, time synchronization and previewing tests. 5. Challenges include performance issues, time management, and platform compatibility.

Error 0x87e107d1 when downloading Xbox content Error 0x87e107d1 when downloading Xbox content Feb 22, 2024 am 09:50 AM

When you encounter error code 0x87e107d1 while downloading Xbox content on your console, you may need some steps to resolve the issue. Usually, this error appears when users try to download content to their Xbox gaming console. Next, we'll explore some ways to fix this issue and ensure you can successfully download the content you need. Fix Error 0x87e107d1 When Downloading Xbox Content If error 0x87e107d1 occurs while downloading Xbox content, use the following fixes to resolve the issue. Check the XboxLive service status Check your internet connection Turn your Xbox console off and on again Try downloading content again Delete and add your profile Let's get started. 1] Check Xbox

Xbox Party Chat Audio Interrupts or Not Working [Fix] Xbox Party Chat Audio Interrupts or Not Working [Fix] Feb 19, 2024 am 11:18 AM

If you encounter problems with chat audio being cut off or not working when using Xbox Party, it may be due to an unstable Internet connection or a failure of the Xbox Live service. This article will help you solve these problems and ensure that you can conduct Xbox Party game chat smoothly. Fix Xbox Party Chat audio cutting out or not working Use these fixes to fix Xbox Party Chat audio cutting out or not working: Check XboxLive service status Check your internet connection Turn your Xbox console off and on again Check your NAT type Leave and rejoin the party Factory Reset your Xbox console and let's get started. 1] Check XboxLive service status and continue troubleshooting

See all articles