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

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

Patricia Arquette
Release: 2024-11-28 22:39:13
Original
379 people have browsed it

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

jQuery 1.9 .live() has been removed

In jQuery 1.9 and later, the .live() function has been deprecated and removed. When upgrading from jQuery 1.8 or earlier, you may encounter the error TypeError: $(...).live is not a function.

Migration Guide to Replace .live()

To replace .live(), use the .on() function instead, but note that the parameters are different:

  • Before: .live(events, function)
  • After: .on(eventType, selector, function)

The child selector is crucial. If not needed, set it to null.

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, if the parent is unknown: $(document).on('click', '.myButton', function)

Additional Resources:

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

The above is the detailed content of How Do I 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