<div id="com-choose" tabindex="4" class="dropdown"> <a href="#" title="连接" id='command-connecting-a' type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> <img src="./static/img/icon/connect.png" alt=""> <span>连接</span> </a> <ul class="dropdown-menu nav-li-hovershow" aria-labelledby="command-connecting-a"></ul> </div>
function searchDevice(e) { $('#com-choose>.nav-li-hovershow').html(''); var deviceList = ipc.sendSync('searchDevice'); switch (deviceList.length) { case 0: // $("#com-choose>.nav-li-hovershow").html("<li>没有查找到设备</li>"); console.log('123'); $("#command-connecting-a span").html('连接') alert('没有查找到设备!'); e.stopPropagation(); //阻止事件 break; default: let exp = /.*((.*))/; let device = '' deviceList.forEach(element => { device += '<li><a href="#">' + element.deviceName.match(exp)[1] + '</a></li>'; // console.log(element); }); $("#com-choose>.nav-li-hovershow").html(device); // $('.device-show-list').toggle(); break; } }
<div class="dropdown"> <a href="#" data-toggle="dropdown"> <i class="fa fa-cog fa-2x"> </i> </a> <div class="dropdown-menu jq22-container"> <div id="treeview" class=""></div> </div> </div>
在dropdown-menu中的需要处理的元素添加 data-stopPropagation=”true”,data-stopPropagation属性是用来对点击时停止传播事件,这样我们的点击事件就不会传播给Bootstrap.js中去了。
调用Bootstrap的stopPropagation()函数,可以阻止元素点击时停止传播事件。
对整个treeview声明一个click事件,并且全部绑定上stopPropagation()方法。
$(".treeview").on("click",function (e) { e.stopPropagation(); })