Javascript jQuery 1.9 .live() 不是函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14354040/
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
jQuery 1.9 .live() is not a function
提问by ngplayground
I recently updated jQuery from 1.8 to 2.1. I suddenly discovered that the .live()stops working.
I get the error TypeError: $(...).live is not a function.
我最近将 jQuery 从 1.8 更新到 2.1。我突然发现.live()停止工作。
我收到错误TypeError: $(...).live is not a function。
Is there any method I can use in place of .live()?
有什么方法可以代替.live()吗?
回答by Samuel Liew
jQuery .live()has been removed in version 1.9 onwards.
jQuery.live()已从 1.9 版开始删除。
That means if you are upgrading from version 1.8 and earlier, you will notice things breaking if you do not follow the migration guide below. You must not simply replace .live()with .on()!
这意味着如果您从 1.8 及更早版本升级,如果您不遵循下面的迁移指南,您会发现事情发生了变化。您不能简单地替换.live()为.on()!
Read before you start doing a search and replace:
在开始搜索和替换之前阅读:
For quick/hot fixeson a live site, do notjust replace the keyword livewith on,
as the parameters are different!
对于快速/热补丁上线的站点,不只是替换关键字live用on,
因为参数是不同的!
.live(events, function)
should map to:
应该映射到:
.on(eventType, selector, function)
The (child) selector is very important! If you do not need to use this for any reason, set it to null.
(子)选择器非常重要!如果您出于任何原因不需要使用它,请将其设置为null.
Migration Example 1:
迁移示例 1:
before:
前:
$('#mainmenu a').live('click', function)
after, you move the child element (a) to the .on()selector:
之后,您将子元素 ( a)移动到.on()选择器:
$('#mainmenu').on('click', 'a', function)
Migration Example 2:
迁移示例 2:
before:
前:
$('.myButton').live('click', function)
after, you move the element (.myButton) to the .on()selector, and find the nearest parent element (preferably with an ID):
之后,您将元素 ( .myButton)移动到.on()选择器,并找到最近的父元素(最好带有 ID):
$('#parentElement').on('click', '.myButton', function)
If you do not know what to put as the parent, bodyalways works:
如果您不知道将什么作为父项,请body始终有效:
$('body').on('click', '.myButton', function)
See also:
也可以看看:
回答by Liran Barniv
You can avoid refactoring your code by including the following JavaScript code
您可以通过包含以下 JavaScript 代码来避免重构您的代码
jQuery.fn.extend({
live: function (event, callback) {
if (this.selector) {
jQuery(document).on(event, this.selector, callback);
}
return this;
}
});
回答by Sirko
The jQuery API documentation lists live()as deprecated as of version 1.7 and removed as of version 1.9: link.
jQuery API 文档列出live()自 1.7 版起已弃用,自 1.9 版起已删除:link。
version deprecated: 1.7, removed: 1.9
不推荐使用的版本:1.7,删除:1.9
Furthermore it states:
此外,它指出:
As of jQuery 1.7, the .live() method is deprecated. Use .on()to attach event handlers. Users of older versions of jQuery should use .delegate()in preference to .live()
从 jQuery 1.7 开始,不推荐使用 .live() 方法。使用.on()附加事件处理程序。旧版本 jQuery 的用户应该优先使用.delegate()而不是 .live()
回答by David Thomas
Forward port of .live()for jQuery >= 1.9
Avoids refactoring JS dependencies on .live()Uses optimized DOM selector context
.live()jQuery >= 1.9 的转发端口避免在.live()使用优化的 DOM 选择器上下文时 重构 JS 依赖
/**
* Forward port jQuery.live()
* Wrapper for newer jQuery.on()
* Uses optimized selector context
* Only add if live() not already existing.
*/
if (typeof jQuery.fn.live == 'undefined' || !(jQuery.isFunction(jQuery.fn.live))) {
jQuery.fn.extend({
live: function (event, callback) {
if (this.selector) {
jQuery(document).on(event, this.selector, callback);
}
}
});
}
回答by Tofeeq
A very simple fix that doesn't need to change your code, just add jquery migration script, download here https://github.com/jquery/jquery-migrate/
一个非常简单的修复,不需要更改您的代码,只需添加 jquery 迁移脚本,在此处下载 https://github.com/jquery/jquery-migrate/
It supplies jquery deprecated but needed functions like "live", "browser" etc
它提供不推荐使用的 jquery 但需要的功能,如“live”、“browser”等
回答by matino
.live was removed in 1.9, please see the upgrade guide: http://jquery.com/upgrade-guide/1.9/#live-removed
.live 在 1.9 中被移除,请参阅升级指南:http: //jquery.com/upgrade-guide/1.9/#live-removed
回答by koopajah
回答by Gerfried
I tend not to use the .on() syntax, if not necessary. For example you can migrate easier like this:
如果没有必要,我倾向于不使用 .on() 语法。例如,您可以像这样更轻松地迁移:
old:
老的:
$('.myButton').live('click', function);
new:
新的:
$('.myButton').click(function)
Here is a list of valid event handlers: https://api.jquery.com/category/forms/
以下是有效事件处理程序的列表:https: //api.jquery.com/category/forms/
回答by fagiani
If you happen to be using the Ruby on Rails' jQuery gem jquery-railsand for some reason you can't refactor your legacy code, the last version that still supports is 2.1.3and you can lock it by using the following syntax on your Gemfile:
如果您碰巧正在使用 Ruby on Rails 的 jQuery gemjquery-rails并且由于某种原因无法重构遗留代码,那么仍然支持的最后一个版本是2.1.3,您可以通过在您的 上使用以下语法来锁定它Gemfile:
gem 'jquery-rails', '~> 2.1', '= 2.1.3'
then you can use the following command to update:
那么您可以使用以下命令进行更新:
bundle update jquery-rails
I hope that help others facing a similar issue.
我希望能帮助其他面临类似问题的人。

