Posted in Javascript onSeptember 22, 2017
详解bootstrap用dropdown-menu实现上下文菜单
写在前面:
所谓上下文菜单,它与一般菜单的区别在于:
通过右键触发显示在鼠标右键点击处
鼠标在别处点击后,该菜单消失
实现方法:
在html中定义一个普通的没有触发条件的dropdown-menu,然后写这个menu的父容器的监听即可实现。
代码:
<div id="settingInGraph"> <ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu" id="contextMenu"> <li><a tabindex="-1" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >任务指派</a></li> <li><a tabindex="-1" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >任务监听</a></li> <li><a tabindex="-1" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >任务表单</a></li> <li class="divider"></li> <li><a tabindex="-1" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >...</a></li> </ul> </div>
$("#settingInGraph").mousedown(function(e) { if (3 == e.which) { document.oncontextmenu = function() {return false;} $("#contextMenu").hide(); $("#contextMenu").attr("style","display: block; position: fixed; top:" + e.pageY + "px; left:" + e.pageX + "px; width: 180px;"); $("#contextMenu").show(); } }); $("#settingInGraph").click(function(e) { $("#contextMenu").hide(); });
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
详解bootstrap用dropdown-menu实现上下文菜单
- Author -
lynnlysh声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@