jQuery“对象函数(e,t){return new v.fn.init(e,t,n)}没有方法’on’”

我正在关注@coreyward的例子, 它将对象编辑表单放在Rails中的modal dialog窗口中,这在本答案中有所概述,并在此要点中针对rails 3.1进行了更新 。

我正在使用Rails 3.2.8和jQuery-Rails 2.1.3(jQuery 1.8.2正在应用程序中加载)。 然而,这个来自要点的coffeescript引起了第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 

关于这个问题的评论让我知道这个jQuery版本问题可能是原因,这就是为什么我检查了我的jQuery版本,但显然这个jQuery并不像解决这个问题那样古老。 我还validation了在此脚本之前加载了jQuery(或者至少,它在加载顺序中位于此脚本之上)。

我把上面的coffeescript放到一个演示问题的小提琴中 (请参阅控制台查看错误)。 (这也让我觉得这不是我的设置的问题。)我会尝试不同版本的jQuery,看看他们是否解决了它,但也许代码只是不稳定? 我不太清楚coffeescript看到其他人可能会看到的错误。

$(document).on替换$.on会删除错误…… on方法需要从我所知道的DOM元素上调用,之前从未在jQuery根对象上调用它…