【问题标题】:jQuery "Object function (e,t){return new v.fn.init(e,t,n)} has no method 'on'"jQuery“对象函数(e,t){return new v.fn.init(e,t,n)}没有方法'on'”
【发布时间】:2012-11-09 22:59:34
【问题描述】:

我正在关注 @coreyward 的示例,将对象编辑表单放入 Rails 的模式对话框窗口中,即 outlined in this answerupdated for rails 3.1 in this gist

我正在使用 Rails 3.2.8 和 jQuery-Rails 2.1.3(应用程序中加载的是 jQuery 1.8.2)。然而,这个来自 gist 的咖啡脚本在第 7 行(就在评论之后)的标题中提出了错误。

$ ->
  $modal = $('#modal')
  $modal_close = $modal.find('.close')
  $modal_container = $('#modal-container')

  # Handle modal links with the data-remote attribute
  $.on 'ajax:success', 'a[data-remote]', (xhr, data, status) ->
    $modal
      .html(data)
      .prepend($modal_close)
      .css('top', $(window).scrollTop() + 40)
      .show()
    $modal_container.show();

  $.on 'click', '#modal .close', ->
    $modal_container.hide()
    $modal.hide()
    false

this question 上的评论让我觉得这是一个 jQuery 版本问题,这可能是我检查 jQuery 版本的原因,但显然这个 jQuery 没有解决这个问题的那个旧。我还验证了 jQuery 是在此脚本之前加载的(或者至少,它在加载顺序中位于此脚本之上)。

我将上面的咖啡脚本放入a fiddle which demonstrates the problem(请参阅您的控制台以了解错误)。 (这也让我认为这不是我的设置的问题。)我会尝试不同版本的 jQuery 看看他们是否解决了它,但也许代码只是不稳定?我对coffeescript的了解还不够,无法看到对其他人来说可能很明显的错误。

【问题讨论】:

    标签: jquery coffeescript ruby-on-rails-3.2


    【解决方案1】:

    $.on 替换为$(document).on 可消除错误...据我所知,需要在DOM 元素上调用on 方法,之前从未见过它在jQuery 根对象上调用过...

    【讨论】:

    • 我实际上会更新那个要点——我的疏忽,因为我没有复制粘贴代码。
    猜你喜欢
    • 2016-11-15
    • 2012-05-07
    • 2017-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-12
    • 2016-08-02
    相关资源
    最近更新 更多