Turning live() into on() in jQuery

后端 未结 5 2202
盖世英雄少女心
盖世英雄少女心 2020-11-21 22:28

My application has dynamically added Dropdowns. The user can add as many as they need to.

I was traditionally using jQuery\'s live() method to detect w

5条回答
  •  不思量自难忘°
    2020-11-21 23:24

    In addition to the selected answer,

    Port jQuery.live to jQuery 1.9+ while you wait for your application to migrate. Add this to your JavaScript file.

    // Borrowed from jQuery 1.8.3's source code
    jQuery.fn.extend({
      live: function( types, data, fn ) {
              if( window.console && console.warn ) {
               console.warn( "jQuery.live is deprecated. Use jQuery.on instead." );
              }
    
              jQuery( this.context ).on( types, this.selector, data, fn );
              return this;
            }
    });
    

    Note: Above function will not work from jQuery v3 as this.selector is removed.

    Or, you can use https://github.com/jquery/jquery-migrate

提交回复
热议问题