jquery对象没有方法直播

时间:2013-03-05 08:11:58

标签: javascript jquery live

你好我在jquery中有一个叠加层,我只使用版本1.6但是我想添加最新的库1.9.1并且我有这个错误,Uncaught TypeError:Object [object Object]没有方法'live'在我的代码第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);

3 个答案:

答案 0 :(得分:17)

从v1.9开始,

live was removed from 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

答案 1 :(得分:0)

如上所述,自v1.9起已被删除:http://api.jquery.com/live/

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

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

答案 2 :(得分:0)

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