【问题标题】:Rails 4 - Bootstrap Popovers - js not workingRails 4 - Bootstrap Popovers - js不工作
【发布时间】:2016-04-08 01:16:10
【问题描述】:

我试图弄清楚如何让弹出框在我的 Rails 4 应用程序中工作。

我的应用中有 bootstrap-sass gem。根据文档,我在 application.js 文件中包含以下内容:

/= require jquery
//= require bootstrap

我也尝试过将 require bootstrap 换成 require bootstrap-sprockets(但没有区别)。

我的 javascripts 文件夹中有另一个名为 organizations.js.coffee 的文件,其中包含:

$('label-with-popover').popover(placement: 'right')

在我看来,我正在尝试让弹出框工作,我有:

<button type="button"  class="label-with-popover" class="btn btn-default" data-toggle="popover" data-placement="top" data-content="netwerewerwrwe" data-title="test"><i class="fa fa-info-circle"></i></button>

当我尝试这个时,当我点击它时,按钮什么也不做。

任何人都可以看到需要发生什么才能使其正常工作吗?我不确定是否应该在咖啡文件中的行周围放置脚本标签。我试过了,但对这个问题没有影响。

【问题讨论】:

  • 你应该只定义一次类 - 即class="label-with-popover btn btn-default"。有些浏览器只会使用最后一个类语句,这可能是 jquery 找不到它的原因?

标签: javascript jquery ruby-on-rails twitter-bootstrap popover


【解决方案1】:

您没有在 jQuery 中使用正确的选择器,因此弹出框未应用于正确的类。你需要:

$('.label-with-popover').popover(placement: 'right');

在您选择一个名为label-with-popover 的元素之前,它并不存在。您需要选择课程.label-with-popover

【讨论】:

  • 我试过了,还是不行。当我点击按钮时没有任何反应。我可以在控制台检查器中看到此错误消息:10TypeError: $ is not a function。 (在 '$(document)' 中,'$' 是未定义的)
  • 那么你没有 jQuery。不确定以上是否是拼写错误,但您需要它是 2 个斜杠://= require jquery
  • 这是一个错字。我有两个斜线。我如何使 jQuery 工作?我还有其他 jQuery 资产在工作。
  • 你的 gemfile 中有 gem 'jquery-rails' 吗?
  • 但是你说$(document) 说$ 是未定义的。你工作的 jQuery 资源在哪里?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-30
  • 2014-03-24
  • 1970-01-01
  • 2015-07-07
  • 2014-10-16
  • 1970-01-01
  • 2018-12-16
相关资源
最近更新 更多