jQuery 未捕获的类型错误:对象 [object Object] 没有方法“live”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16218120/
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
Uncaught TypeError: Object [object Object] has no method 'live'
提问by Francesca
Getting this error:
收到此错误:
Uncaught TypeError: Object [object Object] has no method 'live'
From this JavaScript and jQuery code:
从这个 JavaScript 和 jQuery 代码:
init: function(options) {
var form = this;
if (!form.data('jqv') || form.data('jqv') == null ) {
options = methods._saveOptions(form, options);
// bind all formError elements to close on click
$(".formError").live("click", function() {
//Getting error here:
//Uncaught TypeError: Object [object Object] has no method 'live'
});
}
return this;
};
Why is method live
missing?
为什么live
缺少方法?
回答by Naftali aka Neal
.live
was removedin jquery 1.9
.live
在 jquery 1.9 中被移除
See DOCs: http://api.jquery.com/live/
请参阅文档:http: //api.jquery.com/live/
Try using .on
instead:
尝试使用.on
:
$(document).on('click', '.formError', function(){
//your event function
});
回答by Ja?ck
According to the documentation, .live()
has been deprecated since 1.7 and removedin 1.9.
根据文档,.live()
自 1.7 以来已被弃用并在 1.9 中删除。
You would either have to downgrade jQuery or use a newer version of the validation plugin, if it's available.
您要么必须降级 jQuery,要么使用更新版本的验证插件(如果可用)。
回答by Richard P.
.live() removed
.live() 移除
The .live() method has been deprecated since jQuery 1.7 and has been removed in 1.9. We recommend upgrading code to use the .on() method instead.
.live() 方法自 jQuery 1.7 起已被弃用,并已在 1.9 中删除。我们建议升级代码以使用 .on() 方法。
To exactly match
完全匹配
$("a.foo").live("click", fn)
You should write
你应该写
$(document).on("click", "a.foo", fn).
For more information, see the .on() documentation. In the meantime, the jQuery Migrate plugin can be additionally used to restore the .live() functionality.
有关更多信息,请参阅.on() 文档。同时,还可以使用 jQuery Migrate 插件来恢复 .live() 功能。
回答by Ciaran Bruen
There's a migrate library that helps you transition from previous versions of jQuery when upgrading: jQuery migrate plugin. You need to include it in your source after jQuery. From the jQuery site:
有一个 migrate 库可帮助您在升级时从先前版本的 jQuery 过渡:jQuery migrate plugin。您需要在 jQuery 之后将它包含在您的源代码中。从 jQuery 站点:
The uncompressed development version of the jQuery Migrate plugin includes console log output to warn when specific deprecated and/or removed features are being used. This makes it valuable as a migration debugging tool for finding and remediating issues in existing jQuery code and plugins. It can be used for its diagnostics with versions of jQuery core all the way back to 1.6.4.
The compressed version of the plugin does not generate any log output, and can be used on production sites when jQuery 1.9 or higher is desired but older incompatible jQuery code or plugins must also be used. Ideally this would only be used as a short-term solution, but that's a decision for you to make.
jQuery Migrate 插件的未压缩开发版本包括控制台日志输出,以在使用特定弃用和/或删除的功能时发出警告。这使得它作为一种迁移调试工具很有价值,用于查找和修复现有 jQuery 代码和插件中的问题。它可以用于 jQuery 核心版本的诊断,一直到 1.6.4。
该插件的压缩版本不生成任何日志输出,当需要 jQuery 1.9 或更高版本但还必须使用较旧的不兼容 jQuery 代码或插件时,可以在生产站点上使用。理想情况下,这只会用作短期解决方案,但这是您做出的决定。