jQuery 1.9 .live() is not a function

前端 未结 10 1064
再見小時候
再見小時候 2020-11-22 03:46

I recently updated jQuery from 1.8 to 2.1. I suddenly discovered that the .live() stops working.
I get the error TypeError: $(...).live is not a funct

相关标签:
10条回答
  • 2020-11-22 04:12

    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 (child) 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)
    

    See also:

    • jQuery - how to use the “on()” method instead of “live()”?
    • jQuery 1.9 Migration Guide
    0 讨论(0)
  • 2020-11-22 04:15

    .live() was deprecated and has now been removed from jQuery 1.9 You should use .on()

    0 讨论(0)
  • 2020-11-22 04:24

    When i will getting this error on my site .it will stop some functionality on my site, after research i find the solution for this problem ,

    $colorpicker_inputs.live('focus', function(e) {
        jQuery(this).next('.farb-popup').show();
        jQuery(this).parents('li').css( {
            position : 'relative',
            zIndex : '9999'
        })
        jQuery('#tabber').css( {
            overflow : 'visible'
        });
    });
    
    $colorpicker_inputs.live('blur', function(e) {
        jQuery(this).next('.farb-popup').hide();
        jQuery(this).parents('li').css( {
            zIndex : '0'
        })
    });
    

    Should be replace 'live' to 'on' check below

        $colorpicker_inputs.on('focus', function(e) {
        jQuery(this).next('.farb-popup').show();
        jQuery(this).parents('li').css( {
            position : 'relative',
            zIndex : '9999'
        })
        jQuery('#tabber').css( {
            overflow : 'visible'
        });
    });
    
    $colorpicker_inputs.on('blur', function(e) {
        jQuery(this).next('.farb-popup').hide();
        jQuery(this).parents('li').css( {
            zIndex : '0'
        })
    });
    

    One more basic exmaple below :

    .live(event, selector, function) 
    

    Change it to :

    .on(event, selector, function)
    
    0 讨论(0)
  • 2020-11-22 04:25

    .live was removed in 1.9, please see the upgrade guide: http://jquery.com/upgrade-guide/1.9/#live-removed

    0 讨论(0)
提交回复
热议问题