Bootstrap Dropdown对我不起作用

时间:2013-10-16 12:35:08

标签: css twitter-bootstrap

我在OS X上使用Chrome,当我进行简单的vanilla Bootstrap测试时,它无效。

我使用常规的v2.3.2文件downloaded from here,然后重新创建了一个vanilla测试。

这就是我的index.html看起来的样子:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8" />
    <title>Test</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="" />
    <meta name="author" content="" />

    <link href="css/bootstrap.css" rel="stylesheet" />
    <link href="css/bootstrap-responsive.css" rel="stylesheet" />

    <script src="js/jquery-1.7.2.min.js"></script>
    <script src="js/bootstrap.js"></script>

<script>
    $('.dropdown-toggle').dropdown()
</script>

    <!--[if lt IE 9]>
      <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
</head>

<body>

    <h2>Hello</h2>

    <p>There should be a dropdown right below this paragraph.</p>

    <div class="row">
        <div class="span12">
            <ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu">
              <li><a tabindex="-1" href="#">Action</a></li>
              <li><a tabindex="-1" href="#">Another action</a></li>
              <li><a tabindex="-1" href="#">Something else here</a></li>
              <li class="divider"></li>
              <li><a tabindex="-1" href="#">Separated link</a></li>
            </ul>
        </div>
    </div>


</body>
</html>

当我这样做时,这就是我所看到的:

Bootstrap Dropdown Not Working

然而在源头,我看到了这一点:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8" />
    <title>Test</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="" />
    <meta name="author" content="" />

    <link href="css/bootstrap.css" rel="stylesheet" />
    <link href="css/bootstrap-responsive.css" rel="stylesheet" />

    <script src="js/jquery-1.7.2.min.js"></script>
    <script src="js/bootstrap.js"></script>

    <!--[if lt IE 9]>
      <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
</head>

<body>

    <h2>Hello</h2>

    <p>There should be a dropdown right below this paragraph.</p>

    <div class="row">
        <div class="span12">
            <ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu">
              <li><a tabindex="-1" href="#">Action</a></li>
              <li><a tabindex="-1" href="#">Another action</a></li>
              <li><a tabindex="-1" href="#">Something else here</a></li>
              <li class="divider"></li>
              <li><a tabindex="-1" href="#">Separated link</a></li>
            </ul>
        </div>
    </div>


</body>
</html>

为什么这不起作用?

2 个答案:

答案 0 :(得分:2)

除了在无序列表(the <a>)之前包含下拉触发器之外,您还忘记使用具有.dropdown类的元素包装菜单,例如

<div class="row">
    <div class="span12">
        <div class="dropdown">
        <a class="dropdown-toggle" data-toggle="dropdown" href="#">Dropdown trigger</a>
        <ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu">
          <li><a tabindex="-1" href="#">Action</a></li>
          <li><a tabindex="-1" href="#">Another action</a></li>
          <li><a tabindex="-1" href="#">Something else here</a></li>
          <li class="divider"></li>
          <li><a tabindex="-1" href="#">Separated link</a></li>
        </ul>
        </div>
    </div>
</div>

示例:http://jsfiddle.net/BVmUL/73/

答案 1 :(得分:1)

在ul之前添加'a'标记,例如写入http://getbootstrap.com/2.3.2/javascript.html#dropdowns

<div class="row">
    <div class="span12">
        <a class="dropdown-toggle" data-toggle="dropdown" href="#">Dropdown trigger</a>
        <ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu">
          <li><a tabindex="-1" href="#">Action</a></li>
          <li><a tabindex="-1" href="#">Another action</a></li>
          <li><a tabindex="-1" href="#">Something else here</a></li>
          <li class="divider"></li>
          <li><a tabindex="-1" href="#">Separated link</a></li>
        </ul>
    </div>
</div>