Object has no method 'live' - jQuery

2019-01-18 08:34发布

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

7条回答
趁早两清
2楼-- · 2019-01-18 08:38

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

查看更多
劳资没心,怎么记你
3楼-- · 2019-01-18 08:47

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+
查看更多
别忘想泡老子
4楼-- · 2019-01-18 08:48

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.

查看更多
Anthone
5楼-- · 2019-01-18 08:50

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

查看更多
我只想做你的唯一
6楼-- · 2019-01-18 08:52

use .on

<script>
$(document).ready(function(){
    $('.delete').on('click', function(e){
        alert('delete');
        e.preventDefault();
    });
});
</script>
查看更多
祖国的老花朵
7楼-- · 2019-01-18 08:56

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

查看更多
登录 后发表回答