16

收到此错误:

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

从此 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;
};

为什么live缺少方法?

4

4 回答 4

32

.live在 jquery 1.9 中被删除

请参阅文档:http: //api.jquery.com/live/


尝试.on改用:

$(document).on('click', '.formError', function(){ 
   //your event function
});
于 2013-04-25T14:57:49.737 回答
7

根据文档.live()自 1.7 以来已被弃用并在 1.9 中删除。

您要么必须降级 jQuery,要么使用更新版本的验证插件(如果可用)。

于 2013-04-25T14:57:04.493 回答
4

.live() removed

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.

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.

于 2014-02-12T14:17:45.530 回答
1

有一个 migrate 库可以帮助您在升级时从以前的 jQuery 版本过渡:jQuery migrate plugin。您需要在 jQuery 之后将它包含在您的源代码中。从 jQuery 网站:

jQuery Migrate 插件的未压缩开发版本包括控制台日志输出,用于在使用特定已弃用和/或删除的功能时发出警告。这使得它作为一种迁移调试工具很有价值,用于查找和修复现有 jQuery 代码和插件中的问题。它可以用于 jQuery 核心版本的诊断,一直到 1.6.4。

该插件的压缩版本不会生成任何日志输出,并且可以在需要 jQuery 1.9 或更高版本但还必须使用较旧的不兼容 jQuery 代码或插件时在生产站点上使用。理想情况下,这只会用作短期解决方案,但这是由您做出的决定。

于 2013-11-26T10:00:03.603 回答