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

How to Migrate from jQuery\'s Deprecated `.live()` to the `.on()` Method?

Linda Hamilton
Linda HamiltonOriginal
2024-11-21 07:26:13796browse

How to Migrate from jQuery's Deprecated `.live()` to the `.on()` Method?

jQuery 1.9 .live() Error: Migration Guide

From version 1.9 onwards, jQuery has removed the .live() function. If you have recently upgraded from version 1.8 or earlier, you may encounter the error "TypeError: $(...).live is not a function." To resolve this issue, you can migrate to the new .on() method.

Migration Instructions:

The parameters for .live() and .on() differ. The following guidelines will help you migrate:

  1. Replace .live(events, function) with .on(eventType, selector, function).
  2. Move the (child) selector to the .on() selector (set it to null if not used).
  3. If you don't know the parent element, use document as the parent.

Migration Examples:

Example 1:

Before:

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

After:

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

Example 2:

Before:

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

After:

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

Or:

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

Additional Resources:

  • [jQuery - how to use the “on()” method instead of “live()”?](https://stackoverflow.com/questions/8587995/jquery-how-to-use-the-on-method-instead-of-live)
  • [jQuery 1.9 Migration Guide](https://learn.jquery.com/migration/)

The above is the detailed content of How to Migrate from jQuery\'s Deprecated `.live()` to the `.on()` Method?. 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
Previous article:My GitHub HighlightsNext article:My GitHub Highlights