jQuery 1.9 .live() is not a function

前端 未结 10 1233
再見小時候
再見小時候 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:06

    Forward port of .live() for jQuery >= 1.9 Avoids refactoring JS dependencies on .live() Uses optimized DOM selector context

    /** 
     * Forward port jQuery.live()
     * Wrapper for newer jQuery.on()
     * Uses optimized selector context 
     * Only add if live() not already existing.
    */
    if (typeof jQuery.fn.live == 'undefined' || !(jQuery.isFunction(jQuery.fn.live))) {
      jQuery.fn.extend({
          live: function (event, callback) {
             if (this.selector) {
                  jQuery(document).on(event, this.selector, callback);
              }
          }
      });
    }
    

提交回复
热议问题