Dropdown menu with Bootstrap 4 only drops once in

2019-08-16 23:16发布

问题:

I recently updated to Bootstrap 4.0.0, I'm running Rails 5.1.5 and I installed BS as specified in the README in their github.

Furthermore it's been added to package.json for yarn.

In gemfile : gem 'bootstrap', '~> 4.0' gem 'jquery-rails'

In package.json : "bootstrap": "^4.0.0", "popper.js": "^1.12.9", "jquery": "^3.3.0"

I set it in my application.scss : @import "bootstrap";

My app/assets/javascripts/application.js:

//= require rails-ujs
//= require jquery3
//= require popper
//= require bootstrap-sprockets
//= require bootstrap
//= require_tree .

Now I trying a basic exemple from an online doc :

<div class="dropdown">
    <button class="btn btn-primary dropdown-toggle" type="button" id="about-us" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
        About Us
    </button>
    <div class="dropdown-menu" aria-labelledby="about-us">
        <a class="dropdown-item" href="#">Our Story</a>
        <a class="dropdown-item" href="#">Our Team</a>
        <a class="dropdown-item" href="#">Contact Us</a>
    </div>
</div>

The problem is that when I click the button, the menu appears, but if I click again it doesn't goes back, and after that I can't click it anymore, it's like frozen.

回答1:

Try just to include bootstrap script files directly in your HTML file, it should do the thing and I think it shouldn't be a problem for you.

You can get them here https://getbootstrap.com/docs/4.0/getting-started/introduction/
It should like this:

<!--Bootstrap CSS should be in head tag-->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<!--Your dropdown-->
<div class="dropdown">
    <button class="btn btn-primary dropdown-toggle" type="button" id="about-us" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
        About Us
    </button>
    <div class="dropdown-menu" aria-labelledby="about-us">
        <a class="dropdown-item" href="#">Our Story</a>
        <a class="dropdown-item" href="#">Our Team</a>
        <a class="dropdown-item" href="#">Contact Us</a>
    </div>
</div>

<!--Bootstrap script files, they come at the end of your body tag-->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>


Here is a working JSFiddle with those files: https://jsfiddle.net/c5bnsrsy/1/