2013-03-05 44 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插件。您可以使用它来检测已弃用和已删除的功能。

相关问题