1. 程式人生 > >bootstrap的dropdown-menu(下拉選單)點選選項後不關閉的方法

bootstrap的dropdown-menu(下拉選單)點選選項後不關閉的方法

下面的例子是bootstrap下拉選單的例子
1、html

<div class="dropdown">
        <button type="button" class="btn dropdown-toggle" id="dropdownMenu1" data-toggle="dropdown">主題
            <span class="caret"></span>
        </button>
        <ul class="dropdown-menu" role="menu" aria-labelledby
="dropdownMenu1">
<li role="presentation"> <a data-stopPropagation="true" role="menuitem" tabindex="-1" href="#">Java</a> </li> <li role="presentation"> <a data-stopPropagation="true" role="menuitem"
tabindex="-1" href="#">
資料探勘</a> </li> <li role="presentation"> <a role="menuitem" tabindex="-1" href="#">資料通訊/網路</a> </li> <li role="presentation" class="divider"></li> <li role
="presentation">
<a role="menuitem" tabindex="-1" href="#">分離的連結</a> </li> </ul> </div>

2、js

//指定要操作的元素的click事件停止傳播—定義屬性值data-stopPropagation的元素點選時停止傳播事件
    $("body").on('click','[data-stopPropagation]',function (e) {
        e.stopPropagation();
    });

這裡寫圖片描述
主要在a標籤加上屬性 data-stopPropagation=”true”,然後在js加入指定要操作的元素的click事件停止傳播