欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

jquery 版本更新后无live函数的处理 新添加 新增加 新内容 动态内容的元素.TypeError: $(...).live is not a function 有大用

jquery live函数语法

jquery版本更新, 发现一个问题: jq自带的live没有了.
控制台下会有如下的提示:
火狐: TypeError: $(...).live is not a function

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

 

网上的一个说法: 
jQuery 1.9 较之前的版本做了很大的调整,很多函数都不被支持。
例如 live(),die(),toggle(),sub(),$.browser 等等都已经被移除,完整的更新内容见 http://jquery.com/upgrade-guide/1.9/ (英文)。
 
在不改变你网站代码的同时,要使用 1.9 之后的版本,你需要使用 jQuery Migrate(转移、过度),详见 http://blog.jquery.com/2013/01/31/jquery-migrate-1-1-0-released/ (英文)。
Migrate 应该只是暂时性的。为了和 jQuery 保持同步,建议你从现在开始根据 1.9 的改变重写你网站的所有代码。
所以之前你的代码如果是
$("#ele").live("click", function() {
    //...
});
现在要写成
$("#ele").on("click", function() {
    //...
});
动态生成的元素要使用 live,要写成  下面是对的  见下面  正确答案
$(document).on("click", "#ele", function() {   //这里不一定要 document 吧,,也可以是预先存在的父元素
    //...
});


这里不知什么原因 hover 好像不行,只能用  mouseenter

$(document).on("mouseenter", ".threecen_son_menu_categories li a", function() {
$('.xiancai_info').css('display','none');
erji_id = $(this).attr('data');
alert($('#xiancai_info_'+erji_id));
$('#xiancai_info_'+erji_id).css('display','block');
});
欢迎转载,转载请注明来自一手册:http://yishouce.com/article/43.html


来自 http://yishouce.com/article/43.html


TypeError: $(…).live is not a function

 

I have a problem with jquery 1.9.1 . I have searched it but these are not solved my problem.

    $('.sm2_expander').live('click', function() {
    $(this).parent().parent().toggleClass('sm2_liOpen').toggleClass('sm2_liClosed');
    return false;
});

Everybody said that "use 'on' function" but this time my code never work.

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

Edit : Here is my using prject page : draggable link

I have a problem with jquery 1.9.1 . I have searched it but these are not solved my problem.

    $('.sm2_expander').live('click', function() {
    $(this).parent().parent().toggleClass('sm2_liOpen').toggleClass('sm2_liClosed');
    return false;
});

Everybody said that "use 'on' function" but this time my code never work.


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

Edit : Here is my using prject page : draggable link

shareimprove this question

 
2 
The on version of $(".sm2_expander").live("click",func) is $(document).on("click",".sm2_expander",func). Don't know where you got "a.offsite". – Jan Dvorak Mar 22 '13 at 15:09
2 
can you share the html also – Arun P Johny Mar 22 '13 at 15:10
   
need more info. Probably use jsfiddle.net to explain the problem. – prasann Mar 22 '13 at 15:15

正确答案 

In your example you have used the selector a.offsite but there are no elements matching this selector in your page. That might be the reason why it is not working.

$(function(){
    $(document).on('click', '.sm2_expander', function(){
        alert('bye');
        $(this).parent().parent().toggleClass('sm2_liOpen').toggleClass('sm2_liClosed');
    })
})

I think you can shorten this to

$(function(){
    $(document).on('click', '.sm2_expander', function(){
        $(this).closest('li').toggleClass('sm2_liOpen sm2_liClosed');
    })
})
shareimprove this answer
 
   
Thank you. That is my solution. – Enes Pekkaya Mar 22 '13 at 19:03

 

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

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

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

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

I used "jquery-1.8.3.min.js" and my problem solved.

shareimprove this answer
 

Try this out :- http://jsfiddle.net/trdb9/

JS:-

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

HTML:-

<a class="offsite">Click Me</a>
shareimprove this answer
 
   
Thank you for your response but it doesn't work. Example page is : boagworld.com/demos/sitemap – Enes Pekkaya Mar 22 '13 at 15:45
   
Your demo site is using jQuery 1.3 version – Aditya Singh Mar 22 '13 at 15:46
   
Thanks Brother Your code work for me – sachinM Mar 28 '16 at 7:02

you need to use jquery-migrate-1.1.1.min.js or higher. I guess there are big changes coming in jquery, one being making everyone who relied on .live to find new ways. Anyhow try that and it should work.

shareimprove this answer
 

Try replacing live with on in your code.

 $('.sm2_expander').on('click', function() {
    $(this).parent().parent().toggleClass('sm2_liOpen').toggleClass('sm2_liClosed');
    return false;
});
shareimprove this answer
 
   
Thank you for your response but it doesn't work. Example page is : boagworld.com/demos/sitemap – Enes Pekkaya Mar 22 '13 at 15:29

If you have a live site, I will recommend to use jQuery Migrate

https://github.com/jquery/jquery-migrate/

It will automatically add the deprecated but needed functions.

shareimprove this answer
 

protected by Community Sep 21 '16 at 13:00

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count). 

Would you like to answer one of these unanswered questions instead?

来自  http://stackoverflow.com/questions/15573645/typeerror-live-is-not-a-function

普通分类: