最近有需求需要禁用浏览器默认的右键,自定义新的功能,用到了 jQuery contextMenu这个插件,这个插件主要是用于创建右键弹出菜单的jQuery插件
git clone https://github.com/swisnl/jQuery-contextMenu.git
<link rel="stylesheet" href="font-awesome.min.css">
<script type="text/javascript" src="jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="jquery.contextMenu.min.js"></script>
<script type="text/javascript" src="jquery.ui.position.min.js"></script>
<span class="context-menu-one btn btn-default">right click me</span>
jQuery(document).ready(function($) {
// 禁用原生右键功能
$(document).bind("contextmenu",function(e){
return false;
})
// 自定义右键
$('#the-node').contextMenu({
//选择器 定义哪些元素触发此菜单
selector: 'li',
callback: function(key, options) {
var m = "clicked: " + key + " on " + $(this).text();
window.console && console.log(m) || alert(m);
},
//定义菜单 名称和图标
items: {
// 引用了font-awesome图标
"edit": {name: "编辑", icon: "fa-edit fa"},
"cut": {name: "剪切", icon: "cut",},
"copy": {name: "复制", icon: "copy"},
"paste": {name: "粘贴", icon: "paste"},
"delete": {name: "删除", icon: "delete"},
"sep1": "---------",
"quit": {name: "退出", icon: function($element, key, item){ return 'context-menu-icon context-menu-icon-quit'; }}
}
});
});