为什么使用按钮作为选项,引导程序下拉列表无法正常工作?

时间:2017-08-17 05:49:39

标签: jquery twitter-bootstrap drop-down-menu

<div class="dropdown">
 <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenu2" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    Dropdown
 </button>
  <div class="dropdown-menu" aria-labelledby="dropdownMenu2">
    <button class="dropdown-item" type="button">Action</button>
    <button class="dropdown-item" type="button">Another action</button>
    <button class="dropdown-item" type="button">Something else here</button>
  </div>
</div>

我想要下拉列表和带按钮标签的选项。例如https://www.w3schools.com/bootstrap/tryit.asp?filename=trybs_ref_js_dropdown_js&stacked=h

我想用按钮替换锚标签,但它会显示为我不想要的按钮形状。我尝试了https://v4-alpha.getbootstrap.com/components/dropdowns/中的代码,但它没有显示为它的样子。有什么方法可以解决这个问题?感谢。

1 个答案:

答案 0 :(得分:1)

这对您有用。

<强>更新

我刚刚添加了一些额外的 CSS自定义样式来支持您的buttons,这是必需的,因为默认.dropdown-menu>li>a支持该样式这意味着该样式只支持ali内的.dropdown-menu标记,所以我做的是 - 我只是将您的按钮设置为默认样式

我还在 Dropdown 按钮中添加了<span class="caret"></span>,并且还添加了类btn-primary,只有在需要时才使用它。希望这会对你有所帮助。

&#13;
&#13;
$(document).ready(function() {
  $(".dropdown-toggle").dropdown();
});
&#13;
.dropdown-menu>button.dropdown-item {
  display: block;
  padding: 3px 20px;
  clear: both;
  font-weight: 400;
  line-height: 1.42857143;
  color: #333;
  white-space: nowrap;
  width: 100%;
  text-align: left;
  background: #fff;
  border: none;
}

.dropdown-menu>button.dropdown-item:focus,
.dropdown-menu>button.dropdown-item:hover {
  color: #262626;
  text-decoration: none;
  background-color: #f5f5f5;
}
&#13;
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="dropdown">
  <button class="btn btn-primary dropdown-toggle" type="button" id="dropdownMenu2" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    Dropdown <span class="caret"></span>
 </button>
  <div class="dropdown-menu" aria-labelledby="dropdownMenu2">
    <button class="dropdown-item" type="button">Action</button>
    <button class="dropdown-item" type="button">Another action</button>
    <button class="dropdown-item" type="button">Something else here</button>
  </div>
</div>
&#13;
&#13;
&#13;