$().live不是函数 - JavaScript/jQuery

Joh*_*ino 7 javascript jquery function popup

在Firefox中我突然从firebug得到这条消息:

$('a.close, #fade').live is not a function
Run Code Online (Sandbox Code Playgroud)

的确,当我点击图库和弹出窗口时.我无法摆脱它.由于此错误消息,click事件从未注册.

这是脚本:

        $('a.poplight[href^=#]').click(function() {
        var popID = $(this).attr('rel');  
        var popURL = $(this).attr('href');  

        var query= popURL.split('?');
        var dim= query[1].split('&');
        var popWidth = dim[0].split('=')[1];  

        //Fade in the Popup and add close button

        var div_popup = document.createElement('div');
        div_popup.setAttribute('id',popID);
        div_popup.setAttribute('class','popup_block');
        document.body.appendChild(div_popup);

        $(div_popup).fadeIn().css({ 'width': Number( popWidth ) }).prepend('<a href="#" class="close"><img src="close_pop.png" class="btn_close" title="Close Window" alt="Close" /></a> <a href="thumbBg' + $(this).attr('rel').substring($(this).attr('rel').lastIndexOf('p') + 1,$(this).attr('rel').length) + '"></a><p>The Human Diet: By Rene Endara</p>');

        var popMargTop = ($('#' + popID).height() + 80) / 2;
        var popMargLeft = ($('#' + popID).width() + 80) / 2;

        $('#' + popID).css({
            'margin-top' : -popMargTop,
            'margin-left' : -popMargLeft        
        });

        $('body').append('<div id="fade"></div>');  
        $('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); 
        return false;
    });

    //Close Popups and Fade Layer
    $('a.close, #fade').live('click', function() {  
        $('#fade , .popup_block').fadeOut(function() {
            $('#fade, a.close').remove();  //fade them both out
        });
        return false;
    });
Run Code Online (Sandbox Code Playgroud)

标记:

    <ul class="thumb">
    <li><a href="#?w=500" rel="popup1" class="poplight"><img src="images/thumb1.jpg" alt="" /></a></li>
    <li><a href="#?w=500" rel="popup2" class="poplight"><img src="images/thumb2.jpg" alt="" /></a></li>
    <li><a href="#?w=500" rel="popup3" class="poplight"><img src="images/thumb3.jpg" alt="" /></a></li>
    <li><a href="#?w=500" rel="popup4" class="poplight"><img src="images/thumb4.jpg" alt="" /></a></li>
   </ul>
Run Code Online (Sandbox Code Playgroud)

谢谢你的回复.

Abh*_*hta 27

http://api.jquery.com/live/

自从.live()jQuery 1.7+开始不推荐使用,你必须使用.on()或者.delegate().

请参阅相关问题jQuery 1.9 .live()不是如何迁移现有代码的函数.

  • 现在这很重要,因为它已在1.9中完全删除 (5认同)

Sco*_*ell 9

.live()是在jQuery 1.3中引入的,因此它不适用于早期版本.

.live() 此后也在jQuery 1.7以后被弃用了.

替代品是.on().delegate()

请参阅相关问题jQuery 1.9 .live()不是如何迁移现有代码的函数.