详解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(); });
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。