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
Patricia ArquetteOriginal
2024-11-24 06:15:10303browse

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)

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)

After:

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

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

Migration Example 2:

Before:

$('.myButton').live('click', function)

After:

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

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

$(document).on('click', '.myButton', function)

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!

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