Skip to content

jQuery 1.9 .live() is not a function

🏷️ jQuery


原文:http://stackoverflow.com/questions/14354040/jquery-1-9-live-is-not-a-function


jQuery .live() has been removed in version 1.9 onwards.

That means if you are upgrading from version 1.8 and earlier, you will notice things breaking if you do not follow the migration guide below. You must not simply replace .live() with .on()!

Read before you start doing a search and replace:

For quick/hot fixes on a live site, do not just replace the keyword live with on,
as the parameters are different! .live(events, function) should map to: .on(eventType, selector, function)
The selector is very important! If you do not need to use this for any reason, set it to null.

Migration Example 1:

before: $('#mainmenu a').live('click', function)
after, you move the child element (a) to the .on() selector: $('#mainmenu').on('click', 'a', function)

Migration Example 2:

before: $('.myButton').live('click', function)
after, you move the element (.myButton) to the .on() selector, and find the nearest parent element (preferably with an ID): $('#parentElement').on('click', '.myButton', function)
If you do not know what to put as the parent, body always works: $('body').on('click', '.myButton', function)