最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Twitter Bootstrap dropdown not working - Stack Overflow

programmeradmin5浏览0评论

Any ideas why this Twitter Bootstrap dropdown isn't working?

Javascript in head:

<script src="/assets/js/bootstrap.js" type="text/javascript"></script>

Menu HTML:

             <li class="dropdown">
                <a data-target="#" data-toggle="dropdown" class="dropdown-toggle" href="#">Dropdown menu here...
                  <b class="caret"></b>
                </a>
                <ul class="dropdown-menu">
                  <li><a href="/app/1">Item A</a></li>
                  <li><a href="/app/2">Item B</a></li>
                  <li><a href="/app/3">Item C</a></li>
                </ul>
              </li>

Any ideas why this Twitter Bootstrap dropdown isn't working?

Javascript in head:

<script src="/assets/js/bootstrap.js" type="text/javascript"></script>

Menu HTML:

             <li class="dropdown">
                <a data-target="#" data-toggle="dropdown" class="dropdown-toggle" href="#">Dropdown menu here...
                  <b class="caret"></b>
                </a>
                <ul class="dropdown-menu">
                  <li><a href="/app/1">Item A</a></li>
                  <li><a href="/app/2">Item B</a></li>
                  <li><a href="/app/3">Item C</a></li>
                </ul>
              </li>
Share Improve this question asked Sep 21, 2012 at 17:37 SnowcrashSnowcrash 86.2k99 gold badges283 silver badges411 bronze badges 4
  • 3 In what order is your js loaded in? jQuery goes first as always, bootstrap second. – Andres I Perez Commented Sep 21, 2012 at 19:58
  • If you have bootstrap.js (not bootstrap.min.js), you should not also add bootstrap-dropdown.js. That did the trick for me. – Fahrenheit Commented Jan 22, 2013 at 1:00
  • 1 Try add to dom-ready: $('.dropdown-toggle').dropdown(); – user956584 Commented Jan 29, 2013 at 16:48
  • You don't need to tie it to the document ready function. It works without any JavaScript. – Graham Commented Jun 17, 2013 at 14:09
Add a ment  | 

3 Answers 3

Reset to default 7

Put the dropdown class on a div that is wrapping the li rather than on the li itself. Like this...

        <div class="dropdown">
        <a class="dropdown-toggle" data-toggle="dropdown" href="#">Dropdown trigger</a>
        <ul class="dropdown-menu" role="menu" aria-labelledby="dLabel">
         ...
        </ul>
        </div>

That example is from the bootstrap site

Your code should look like this...

    <!DOCTYPE html >
    <head>
    <script src="//ajax.googleapis./ajax/libs/jquery/1.8.1/jquery.min.js"></script>
    <script type="text/javascript" src="../plugins/js/bootstrap-dropdown.js"></script>
    <link href="../theme/bootstrap.css" rel=stylesheet type="text/css">

    </head>
    <div class="dropdown">
        <ul class="dropdown">
        <a data-target="#" data-toggle="dropdown" class="dropdown-toggle" href="#">Dropdown menu here...
          <b class="caret"></b>
        </a>
            <ul class="dropdown-menu">
              <li><a href="/app/1">Item A</a></li>
              <li><a href="/app/2">Item B</a></li>
              <li><a href="/app/3">Item C</a></li>
            </ul>
        </ul>
    </div>

I tried making a nested list, but it didn't work with bootstrap. I think it gets the two dropdowns confused.

Make sure you are using the latest version of JQuery

This is easily solved. The element wrapping your code is a li, but it needs to be a div. This works for me:

<div class="dropdown">
  <a data-target="#" data-toggle="dropdown" class="dropdown-toggle" href="#">Dropdown menu here...
    <b class="caret"></b>
  </a>
  <ul class="dropdown-menu">
    <li><a href="/app/1">Item A</a></li>
    <li><a href="/app/2">Item B</a></li>
    <li><a href="/app/3">Item C</a></li>
  </ul>
</div>

If you want it to look like a button, just add "btn" to the class list like this:

<a data-target="#" data-toggle="dropdown" class="dropdown-toggle btn" href="#">

My header section looks like this:

<link rel='stylesheet' type='text/css' href='css/bootstrap.min.css'>
<link rel='stylesheet' type='text/css' href='css/bootstrap-responsive.min.css'>
<script src='js/jquery-2.0.0.min.js' type='text/javascript'>
<script src='js/bootstrap.min.js' type='text/javascript'>

You may want to leave out the responsive css if you're not doing anything for mobile devices.

发布评论

评论列表(0)

  1. 暂无评论