Object has no method 'live' - jQuery

大城市里の小女人 提交于 2019-11-27 03:01:53

问题


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

I just don't see the problem?


回答1:


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

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

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




回答2:


  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.js or http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js since they will automatically update when a new version of jQuery is released and your site will suddenly break if it is not compatible.




回答3:


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




回答4:


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.




回答5:


See on 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+



回答6:


use .on

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



回答7:


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

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



来源:https://stackoverflow.com/questions/14526033/object-has-no-method-live-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!