Home > Web Front-end > JS Tutorial > body text

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

Linda Hamilton
Release: 2024-11-21 07:26:13
Original
763 people have browsed it

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)
Copy after login

After:

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

Example 2:

Before:

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

After:

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

Or:

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

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!

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