自 jQuery 1.7+ 起,.live() 方法已弃用
解决办法:
请参阅VERSION.md 以查看哪些版本的 jquery-rails 捆绑了哪些版本的 jQuery。
例如,您可以使用 1.0.17 之前的任何 jquery-rails gem,一切都会正常工作。例如:
gem 'jquery-rails', '1.0.16' # This match the jQuery version 1.6.4
也就是说,您应该始终使用最新版本的 jQuery,因为每次迭代都提供了大量错误修复和更好的跨浏览器兼容性。请参阅下文如何处理此问题。
延伸阅读:
从 jQuery 1.7+ 开始,您应该使用 .on() 来附加事件处理程序。该文档甚至建议旧版本的 jQuery 使用 .delegate() 而不是 .live()
不再推荐使用.live() 方法,因为后来的jQuery 版本提供了没有缺点的更好的方法。
根据其继承者重写.live() 方法很简单;这些是所有三种事件附件方法的等效调用的模板:
$( selector ).live( events, data, handler ); // jQuery 1.3+
$( document ).delegate( selector, events, data, handler ); // jQuery 1.4.3+
$( document ).on( events, selector, data, handler ); // jQuery 1.7+
如果您想提高对该主题的了解,请继续阅读 old .live() 方法和 new .on() 方法的官方文档。
转换示例:
以下三个方法调用在功能上是等效的:
$( "a.offsite" ).live( "click", function() {
alert( "Goodbye!" ); // jQuery 1.3+
});
$( document ).delegate( "a.offsite", "click", function() {
alert( "Goodbye!" ); // jQuery 1.4.3+
});
$( document ).on( "click", "a.offsite", function() {
alert( "Goodbye!" ); // jQuery 1.7+
});