Why doesn't this bootstrap dropdown example wo

2020-02-12 01:37发布

问题:

I'm just trying out the example in Bootstrap documentation, but it doesn't seem to work. I just get plain HTML. This is my code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" href="bootstrap.css" type="text/css">
<script type="text/javascript" src="http://twitter.github.com/bootstrap/assets/js/bootstrap-dropdown.js"></script>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>

$('.dropdown-toggle').dropdown();


</script>
<title>Dropdown</title>
</head>

<ul class="nav nav-pills">
  <li class="active"><a href="#">Regular link</a></li>
  <li class="dropdown" id="menu1">
    <a class="dropdown-toggle" data-toggle="dropdown" href="#menu1">
      Dropdown
      <b class="caret"></b>
    </a>
    <ul class="dropdown-menu">
      <li><a href="#">Action</a></li>
      <li><a href="#">Another action</a></li>
      <li><a href="#">Something else here</a></li>
      <li class="divider"></li>
      <li><a href="#">Separated link</a></li>
    </ul>
  </li>
  ...
</ul>

</body>
</html>

Does anyone see the problem? Thanks.

回答1:

I see two problems with your markup.

First, you're including the bootstrap plugin "before" the jquery plugin. Jquery first, bootstrap plugins second.

Second, you're using a DOCTYPE that is not supported by the bootstrap, you have to use HTML5's DOCTYPE:

<!DOCTYPE html>


回答2:

I think jquery.js should be imported before the bootstrap javascript file



回答3:

I dont see your <body> opening tag



回答4:

Working demo http://jsfiddle.net/jyy96/2/ or http://jsfiddle.net/jyy96/2/show or http://jsfiddle.net/jyy96/

EDIT 9th July 2015 -Fixed here: http://jsfiddle.net/jyy96/792/ or http://jsfiddle.net/jyy96/792/show/

==========================================

Looks like bootstrapcdn is the popular choice, but for those wanting to use cdnjs.com, they have "all the files" and keep them up to date (version 2.0.0 through 2.1.1).

http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.1.1/css/bootstrap.css
http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.1.1/css/bootstrap.min.css
http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.1.1/css/bootstrap-responsive.css
http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.1.1/css/bootstrap-responsive.min.css
http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.1.1/bootstrap.js
http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.1.1/bootstrap.min.js
http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.1.1/img/glyphicons-halflings-white.png
http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.1.1/img/glyphicons-halflings.png

==========================================

Hope this help.

script

  <script type="text/javascript" src="/js/lib/bootstrap-2.0.2.js"></script>


  <link rel="stylesheet" type="text/css" href="http://twitter.github.com/bootstrap/assets/css/bootstrap.css">

Jquery code

   $('.dropdown-toggle').dropdown();

[1] https://github.com/twbs/bootstrap/issues/1679