如何使用bootstrap为<li>创建下拉列表

时间:2015-04-24 21:20:42

标签: html css twitter-bootstrap

我需要为一个<li>元素下拉

<div class="head-nav">
                <span class="menu"></span>
                <ul class="cl-effect-15">
                    <li><a href="index.php" data-hover="HOME">HOME</a></li>
                    <li><a href="about.php">ABOUT</a></li>
                    <li><a href="contact.php" data-hover="CONTACT">CONTACT</a></li>
                    <li><a href="#" data-hover=" "> </a></li>
                    <li><a href="3" data-hover=" "> </a></li>
                    <li class="pull-right"><a href="#" data-hover="More option" tabindex="-1">More options</a>
                            <ul class="dropdown-menu">
                                <li><a href="#" data-hover=" ">one</a></li>
                                <li><a href="3" data-hover=" ">two</a></li>
                            </ul>
                    </li>
                    <li class="pull-right"><a href="logout.php" data-hover="logout">logout</a></li>
                    <div class="clearfix"> </div>
                </ul>
            </div>

但子菜单无法打开

使用Bootstrap v3.1.1

注意:即使在jquery之后添加也不起作用

3 个答案:

答案 0 :(得分:7)

You have to use

<li class="dropdown">
   <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Dropdown <span class="caret"></span></a>
    <ul class="dropdown-menu" role="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>
       <li class="divider"></li>
       <li><a href="#">One more separated link</a></li>
    </ul>
</li>

In your code, you miss class "dropdown" for li tag. For correct dropdown, you have to use this sintax:

<li class="dropdown">
 <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Item Dropdown <span class="caret"></span></a>
  <ul class="dropdown-menu" role="menu">
<!--- Put your menu-item here -->
  </ul>
</li>

You can refer the Bootstrap documentation here

The issue is causing by the missing of data-toggle="dropdown" in tag a

答案 1 :(得分:1)

Maybe you search this solution: JSBin

For example: You need add the content: attr(data-hover); on your before element. HTML:

<li class="HOVER"><a href="#" data-hover="More option" tabindex="-1">More options</a>

CSS:

.HOVER {
  border: solid 7px #000;
  padding: 20px;
  text-transform: uppercase;
  font-weight: bold;
  color: #fff;
  position: relative;
  width: 300px;
}

.HOVER:hover a:before {
  content: attr(data-hover);
  color: red;
  display: block;
  background: #000;
  position: absolute;
  top: 100%;
  left: -7px;
  right: -7px;
  padding: 15px; 
}

答案 2 :(得分:0)

You're missing the the following attributes:

class="dropdown-toggle" data-toggle="dropdown"

and those attributes go on the element you want to be the "Hook" for the dropdown list.

Sample markup below:

 <li class="dropdown">
                <a href="#lala" class="dropdown-toggle" data-toggle="dropdown" >
                   Java 
                   <b class="caret"></b>
                </a>
                <ul class="dropdown-menu" role="listbox">
                   <li><a href="#lala1" role="option">jmeter</a></li>
                   <li><a href="#lala2" role="option">EJB</a></li>
                   <li><a href="#lala3" role="option">Jasper Report</a></li>
                   <li class="divider"></li>
                   <li><a href="#" role="option">Separated link</a></li>
                   <li class="divider"></li>
                   <li><a href="#" role="option">One more separated link</a></li>
                </ul>
             </li>

Edit

Here is an example Fiddle; notice how if you remove data-toggle="dropdown", the code won't work . :)