打开后无法启动Bootstrap下拉功能

时间:2016-04-28 02:32:42

标签: jquery twitter-bootstrap

我已经设置了我的代码,以便在打开Bootstrap菜单时运行警报,但我正在努力让警报响起。

$('.dropdown.data-api').on('shown.bs.dropdown', function () {
   alert('it worked!');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>


<div class="dropdown">
    	
    <span data-toggle="dropdown">Click me</span>

    <ul class="dropdown-menu">
        <li>Hello World!
    </ul>
    	
</div>

1 个答案:

答案 0 :(得分:1)

事件选择器正在查找包含dropdowndata-api类的下拉列表。从事件选择器中删除.data-api,或将data-api添加到下拉类列表中。

$('.dropdown.data-api').on('shown.bs.dropdown', function () {
   alert('it worked!');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

<div class="dropdown data-api">
    	
    <span data-toggle="dropdown">Click me</span>

    <ul class="dropdown-menu">
        <li>Hello World!
    </ul>
    	
</div>