1

你好,我在 jquery 中有一个覆盖,我只使用版本 1.6,但我想添加最新的库 1.9.1,我有这个错误,Uncaught TypeError: Object [object Object] has no method 'live' at line 20在我的代码上,我的代码是这样的。

(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

3 回答 3

17

live自 v1.9起已从 jQuery中删除。它已被多个版本弃用(自 v1.7 起)。

以下是文档关于重写它以使用delegateon(我在每个调用上方添加评论)的说法:

.live()根据其后继者重写该方法很简单;这些是对所有三种事件附件方法进行等效调用的模板:

// `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.

于 2013-03-05T08:14:39.463 回答
0

如前所述,它已从 v1.9 中删除:http: //api.jquery.com/live/

但是,您可以将代码重构为:

$( document ).on( 'click', 'a[data-reveal-id]', function(e) { ... });
于 2013-03-05T08:17:10.423 回答
0

还有迁移插件。您可以使用它来检测已弃用和删除的功能。

于 2013-03-05T08:33:13.837 回答