Home > Web Front-end > JS Tutorial > How Do I Migrate from jQuery\'s Deprecated .live() to .on()?

How Do I Migrate from jQuery\'s Deprecated .live() to .on()?

Patricia Arquette
Release: 2024-11-24 06:15:10
Original
355 people have browsed it

How Do I Migrate from jQuery's Deprecated .live() to .on()?

jQuery .live() Function Deprecation

jQuery's .live() method is no longer available in versions 1.9 and later. This can lead to errors when attempting to update jQuery from earlier versions.

Migration from .live() to .on()

To replace .live() without breaking functionality, you need to use proper syntax for the .on() method:

.live(events, function) -> .on(eventType, selector, function)
Copy after login

The main difference is that .on() requires an additional parameter specifying the child selector after the event type. If you don't need a child selector, use null instead.

Migration Examples

Migration Example 1:

Before:

$('#mainmenu a').live('click', function)
Copy after login

After:

$('#mainmenu').on('click', 'a', function)
Copy after login

In this example, the child element (a) needs to be specified in the .on() selector.

Migration Example 2:

Before:

$('.myButton').live('click', function)
Copy after login

After:

$('#parentElement').on('click', '.myButton', function)
Copy after login

When the nearest parent element with an ID is unknown, use the following syntax:

$(document).on('click', '.myButton', function)
Copy after login

Remember to always refer to the jQuery migration guide for more information on transitioning from .live() to .on().

The above is the detailed content of How Do I Migrate from jQuery\'s Deprecated .live() to .on()?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template