2013-03-05 78 views
1

你好我有一個覆蓋在jquery,我的工作只與版本1.6,但我想添加最新的庫1.9.1,我有這個錯誤,未捕獲TypeError:Object [object Object]在我的代碼的第20行沒有「實時」方法,我的代碼是這樣的。jquery對象沒有方法生活

(function($) { 

    $('a[data-reveal-id]').live('click', function(e) { 
     e.preventDefault(); 
     var modalLocation = $(this).attr('data-reveal-id'); 
     $('#'+modalLocation).reveal($(this).data()); 
    }); 


    $.fn.reveal = function(options) { 


     var defaults = { 
      animation: 'fadeAndPop', //fade, fadeAndPop, none 
      animationspeed: 300, //how fast animtions are 
      closeonbackgroundclick: true, //if you click background will modal close? 
      dismissmodalclass: 'close-reveal-modal' //the class of a button or element that will close an open modal 
     }; 

     var options = $.extend({}, defaults, options); 

     return this.each(function() { 

      var modal = $(this), 
       topMeasure = parseInt(modal.css('top')), 
       topOffset = modal.height() + topMeasure, 
       locked = false, 
       modalBG = $('.reveal-modal-bg'); 


      if(modalBG.length == 0) { 
       modalBG = $('<div class="reveal-modal-bg" />').insertAfter(modal); 
      }   


      modal.bind('reveal:open', function() { 
       modalBG.unbind('click.modalEvent'); 
       $('.' + options.dismissmodalclass).unbind('click.modalEvent'); 
       if(!locked) { 
        lockModal(); 
        if(options.animation == "fadeAndPop") { 
         modal.css({'top': $(document).scrollTop()-topOffset, 'opacity' : 0, 'visibility' : 'visible'}); 
         modalBG.fadeIn(options.animationspeed/2); 
         modal.delay(options.animationspeed/2).animate({ 
          "top": $(document).scrollTop()+topMeasure + 'px', 
          "opacity" : 1 
         }, options.animationspeed,unlockModal());     
        } 
        if(options.animation == "fade") { 
         modal.css({'opacity' : 0, 'visibility' : 'visible', 'top': $(document).scrollTop()+topMeasure}); 
         modalBG.fadeIn(options.animationspeed/2); 
         modal.delay(options.animationspeed/2).animate({ 
          "opacity" : 1 
         }, options.animationspeed,unlockModal());     
        } 
        if(options.animation == "none") { 
         modal.css({'visibility' : 'visible', 'top':$(document).scrollTop()+topMeasure}); 
         modalBG.css({"display":"block"}); 
         unlockModal()    
        } 
       } 
       modal.unbind('reveal:open'); 
      });  


      modal.bind('reveal:close', function() { 
       if(!locked) { 
        lockModal(); 
        if(options.animation == "fadeAndPop") { 
         modalBG.delay(options.animationspeed).fadeOut(options.animationspeed); 
         modal.animate({ 
          "top": $(document).scrollTop()-topOffset + 'px', 
          "opacity" : 0 
         }, options.animationspeed/2, function() { 
          modal.css({'top':topMeasure, 'opacity' : 1, 'visibility' : 'hidden'}); 
          unlockModal(); 
         });     
        } 
        if(options.animation == "fade") { 
         modalBG.delay(options.animationspeed).fadeOut(options.animationspeed); 
         modal.animate({ 
          "opacity" : 0 
         }, options.animationspeed, function() { 
          modal.css({'opacity' : 1, 'visibility' : 'hidden', 'top' : topMeasure}); 
          unlockModal(); 
         });     
        } 
        if(options.animation == "none") { 
         modal.css({'visibility' : 'hidden', 'top' : topMeasure}); 
         modalBG.css({'display' : 'none'}); 
        }  
       } 
       modal.unbind('reveal:close'); 
      });  


     modal.trigger('reveal:open') 

      //Close Modal Listeners 
      var closeButton = $('.' + options.dismissmodalclass).bind('click.modalEvent', function() { 
       modal.trigger('reveal:close') 
      }); 

      if(options.closeonbackgroundclick) { 
       modalBG.css({"cursor":"pointer"}) 
       modalBG.bind('click.modalEvent', function() { 
        modal.trigger('reveal:close') 
       }); 
      } 
      $('body').keyup(function(e) { 
       if(e.which===27){ modal.trigger('reveal:close'); } // 27 is the keycode for the Escape key 
      }); 



      function unlockModal() { 
       locked = false; 
      } 
      function lockModal() { 
       locked = true; 
      } 

     }); 
    } 
})(jQuery); 
+4

[版本已棄用:1.7,刪除:1.9](http://api.jquery.com/live/)。 – Vucko 2013-03-05 08:13:05

回答

17

live was removed from jQuery截至v1.9。它已被棄用幾個版本(自v1.7以來)。

這裏是什麼文件說想改寫它使用delegateon(我加了每次調用上述評論):

Rewriting the .live() method in terms of its successors is straightforward; these are templates for equivalent calls for all three event attachment methods:

// `live`, now removed 
$(selector).live(events, data, handler);    // jQuery 1.3+ 

// `delegate`, superceded but not deprecated 
$(document).delegate(selector, events, data, handler); // jQuery 1.4.3+ 

// `on`, the latest 
$(document).on(events, selector, data, handler);  // jQuery 1.7+ 

所以你的情況,而不是:

$('a[data-reveal-id]').live('click', function(e) { // ... 

你想要

$(document).delegate('a[data-reveal-id]', 'click', function(e) { // ... 
// Or 
$(document).on('click', 'a[data-reveal-id]', function(e) { // ... 

但是,通常可以找到更靠近要掛接的元素的容器元素,如果是,則要使用該元素而不是document。例如,假設您所有的data-reveal-id錨都在div中,其中id"stuff"。而不是

$(document).on('click', 'a[data-reveal-id]', function(e) { // ... 

你會更好鉤住單擊有點接近錨:

$('#stuff').on('click', 'a[data-reveal-id]', function(e) { // ... 

但是,如果有超過document其他沒有合適的共同祖先元素,你可以使用document

+0

這已經成爲最近一個最常見的循環jQuery語句之一。 :D – 2013-03-05 08:15:43

+0

和haw我可以改變生活到別的東西?也許你知道一些其他的功能呢? – Arturik1988 2013-03-05 08:16:17

0

如前所述,它被移除V1.9的:http://api.jquery.com/live/

但是,您可以重構代碼,以這樣的:

$(document).on('click', 'a[data-reveal-id]', function(e) { ... }); 
+0

TJ Fogarty非常感謝你,祝你有個美好的一天IvanIvković非常感謝 – Arturik1988 2013-03-05 08:21:38

0

而且裏面還有migrate插件。您可以使用它來檢測已棄用和已刪除的功能。

相關問題