jQuery TypeError: $(...).live 不是函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15573645/
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
TypeError: $(...).live is not a function
提问by Enes Pekkaya
I have a problem with jquery 1.9.1 . I have searched it but these are not solved my problem.
我有 jquery 1.9.1 的问题。我已经搜索过了,但这些都没有解决我的问题。
$('.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.
每个人都说“使用'on'函数”,但这次我的代码从不工作。
$(document).on("click", "a.offsite", function(){ alert("Goodbye!"); });
Edit : Here is my using prject page : draggable link
编辑:这是我使用的项目页面:可拖动链接
回答by Arun P Johny
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.
在您的示例中,您使用了选择器,a.offsite
但页面中没有与此选择器匹配的元素。这可能是它不起作用的原因。
$(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');
})
})
回答by Ipsita Rout
.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 function on how to migrate existing code.
请参阅相关问题 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.
我使用了“jquery-1.8.3.min.js”,我的问题解决了。
回答by Aditya Singh
Try this out :-http://jsfiddle.net/trdb9/
试试这个:- http://jsfiddle.net/trdb9/
JS:-
JS:-
$(document).on("click", "a.offsite", function () {
alert("Goodbye!");
});
HTML:-
HTML:-
<a class="offsite">Click Me</a>
回答by The Lone Coder
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.
您需要使用jquery-migrate-1.1.1.min.js
或更高。我想会有很大的变化jquery
,其中之一是让每个依赖的人都.live
找到新的方法。
Anyhow, try that and it should work.
无论如何,尝试一下,它应该可以工作。
回答by Mohammad Adil
Try replacing live
with on
in your code.
尝试更换live
用on
在你的代码。
$('.sm2_expander').on('click', function() {
$(this).parent().parent().toggleClass('sm2_liOpen').toggleClass('sm2_liClosed');
return false;
});
回答by Tofeeq
If you have a live site, I will recommend to use jQuery Migrate
如果你有一个实时站点,我会推荐使用 jQuery Migrate
https://github.com/jquery/jquery-migrate/
https://github.com/jquery/jquery-migrate/
It will automatically add the deprecated but needed functions.
它将自动添加已弃用但需要的功能。