Bootstrap Tooltip in Ruby on Rails

二次信任 提交于 2019-11-30 04:46:42

问题


I'm trying to get this done for quite some time now and i can't figure out whats wrong:

My Link:

<%= link_to '#', tag, class: "tag-tooltip",
                    :data => {:toggle=>"tooltip"},
                    'data-original-title' => "Hey Whats up",
                    'data-placement' => 'right'%>

My Javascript:

$('.tag-tooltip').tooltip();

But it's not working... What am i missing ?

EDIT

If i go into my Chrome Console and insert ($('.tag-tooltip').tooltip();) it is working.

So i guess the js file is not loading ? (I checked my application.js and i'm requiring everything.)


回答1:


I found out the wrapping which worked:

$(document).on("ready page:change", function() {
  $('.tag-tooltip').tooltip();
});

Update:

$(document).on("turbolinks:load",function(){
  $('.tag-tooltip').tooltip();
})

With vanilla JS

document.addEventListener("turbolinks:load", function() {
  $('.tag-tooltip').tooltip();
});



回答2:


In my case the problem was that I was loading jquery-ui after bootstrap-sprockets

In my application.js I changed:

//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require bootstrap-sprockets
//= require jquery-ui

to

//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require turbolinks
//= require bootstrap-sprockets

Then it all started working as it should.




回答3:


You need to add some script in your code :

$(document).ready(function(){
    $('[data-toggle="tooltip"]').tooltip(
      {container:'body', trigger: 'hover', placement:"right"}
    );   
  });


来源:https://stackoverflow.com/questions/20129970/bootstrap-tooltip-in-ruby-on-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!