Javascript 对象没有方法 'live' - jQuery

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14526033/
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-24 17:06:00  来源:igfitidea点击:

Object has no method 'live' - jQuery

javascriptjquerylive

提问by reggie

<script>
$(document).ready(function(){
    $('.delete').live('click', function(e){
        alert('delete');
        e.preventDefault();
    });
});
</script>
<a href='#' id='_1' class='delete'>Delete</a>

Gives me an error:

给我一个错误:

Uncaught TypeError: Object [object Object] has no method 'live'

未捕获的类型错误:对象 [object Object] 没有方法“live”

I just don't see the problem?

我只是没有看到问题?

回答by diggersworld

.live()is a deprecated function (from 1.7+) and removed completely from jQuery 1.9+.

.live()是一个不推荐使用的函数(从 1.7+ 开始)并从 jQuery 1.9+ 中完全删除。

You can instead use .on()or .bind()methods:

您可以改为使用.on().bind()方法:

http://api.jquery.com/on/
http://api.jquery.com/bind/

http://api.jquery.com/on/
http://api.jquery.com/bind/

回答by Dave Methvin

  1. If the call to .live()is inside your own code, just change it to .on()using the rules shown at http://api.jquery.com/live.

  2. If the code is in a third-party jQuery plugin, use the jQuery Migrate plugin to restore .live()until the author updates their plugin: https://github.com/jquery/jquery-migrate#readme.

  3. In production sites, do not use URLs that reference the "latest" version of jQuery such as http://code.jquery.com/jquery-latest.jsor http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.jssince they will automatically update when a new version of jQuery is released and your site will suddenly break if it is not compatible.

  1. 如果调用.live()在您自己的代码中,只需将其更改为.on()使用http://api.jquery.com/live 中显示的规则。

  2. 如果代码在第三方 jQuery 插件中,请使用 jQuery Migrate 插件恢复,.live()直到作者更新他们的插件:https: //github.com/jquery/jquery-migrate#readme

  3. 在生产站点中,请勿使用引用“最新”版本 jQuery 的 URL,例如http://code.jquery.com/jquery-latest.jshttp://ajax.googleapis.com/ajax/libs/jquery /1/jquery.min.js因为它们会在新版本的 jQuery 发布时自动更新,如果不兼容,您的站点会突然中断。

回答by Learner

If you are using jQuery 1.7+ use on(...)instead of live(...).
Check this: http://api.jquery.com/on/

如果您在使用jQuery 1.7+使用on(...)代替live(...)
检查这个:http: //api.jquery.com/on/

回答by Jinn

There is one scenario when neither .on(), nor .bind() won't work: when the element does not exist when the event handler is being added. And this was what live() did.

有一种情况 .on() 和 .bind() 都不起作用:当添加事件处理程序时元素不存在时。这就是 live() 所做的。

回答by user2466160

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

请参阅http://api.jquery.com/live/

old

老的

$("a.offsite").live("click", function(){ alert("Goodbye!"); });                // jQuery 1.3+
$(document).delegate("a.offsite", "click", function(){ alert("Goodbye!"); });  // jQuery 1.4.3+

new

新的

$(document).on("click", "a.offsite", function(){ alert("Goodbye!"); });        // jQuery 1.7+

回答by GDW

use .on

使用 .on

<script>
$(document).ready(function(){
    $('.delete').on('click', function(e){
        alert('delete');
        e.preventDefault();
    });
});
</script>

回答by Ketan Thakkar

There is a jQuery migrate plugin (use that) ....... it will resolve the issue

有一个 jQuery migrate 插件(使用那个)......它会解决这个问题

ASP.NET MVC ajax-unobtrusive + jQuery 1.9 http://bugs.jquery.com/ticket/13220

ASP.NET MVC ajax-unobtrusive + jQuery 1.9 http://bugs.jquery.com/ticket/13220