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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4083603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 10:31:51  来源:igfitidea点击:

$().live is not a function - JavaScript/jQuery

javascriptjqueryfunctionpopup

提问by JohnMerlino

In Firefox I suddenly got this message from firebug:

在 Firefox 中,我突然收到了来自 firebug 的消息:

$('a.close, #fade').live is not a function

Indeed, when I click the image gallery and popup displays. I cannot close out of it. The click event never registers due to this error message.

确实,当我单击图片库和弹出窗口时。我无法关闭它。由于此错误消息,单击事件永远不会注册。

This is script:

这是脚本:

        $('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;
    });

markup:

标记:

    <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>

Thanks for response.

感谢您的回复。

回答by Abhishek Mehta

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

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

Since .live()is deprecated as of jQuery 1.7+, you have to use either .on()or .delegate().

由于自.live()jQuery 1.7+起已弃用,因此您必须使用.on().delegate()

See related question jQuery 1.9 .live() is not a functionon how to migrate existing code.

请参阅相关问题jQuery 1.9 .live() is not a functionon how to migrate existing code。

回答by Scott Harwell

.live()was introduced in jQuery 1.3, therefore it won't work with earlier versions.

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

.live()has also since been deprecated in jQuery 1.7 onwards.

.live()从 jQuery 1.7 开始,它也被弃用了。

The alternatives are .on()and .delegate()

替代方案是.on().delegate()

See related question jQuery 1.9 .live() is not a functionon how to migrate existing code.

请参阅相关问题jQuery 1.9 .live() is not a functionon how to migrate existing code。

回答by Jo?o Marcos Santos Teixeira

Tente usar .bindno lugar de .live

Tente usar .bindno lugar de.live

Try to use .bindinstead of .live

尝试使用.bind代替.live

回答by Marklar

For anyone else who is using >= v1.9 please see here about depreciation: jQuery 1.9 .live() is not a function

对于使用 >= v1.9 的其他人,请参阅此处有关折旧的信息:jQuery 1.9 .live() is not a function

回答by raviOcs

Find this funcion in wp-includes\js\thickbox\thickbox.js and change the function:

在 wp-includes\js\thickbox\thickbox.js 中找到这个函数并更改函数:

function tb_init(domChunk){
  jQuery(domChunk).live('click', tb_click);
}

Replacing the "live" method for "on", like this:

将“live”方法替换为“on”,如下所示:

function tb_init(domChunk){
    jQuery(domChunk).on('click',tb_click);
}

If you are using Nivo-Slider, the jquery.nivo.slider.js will need similar changes.

如果您使用 Nivo-Slider,则 jquery.nivo.slider.js 将需要类似的更改。