在“复制”、“粘贴”等上下文菜单命令中实现并不容易,因此我决定根据您的上一个问题从the answer 修改我的演示。在the new demo 中,上下文菜单仅在页面不包含选定文本时才会出现。
第一个问题是jquery.contextmenu.js的原始代码包含以下代码片段:
$(this).bind('contextmenu', function(e) {
// Check if onContextMenu() defined
var bShowContext = (!!hash[index].onContextMenu) ? hash[index].onContextMenu(e) : true;
if (bShowContext) display(index, this, e, options);
return false;
});
所以contextmenu 处理程序总是返回false 并阻止创建标准上下文菜单。我将代码修复为以下(您可以下载完整的修改代码here):
$(this).bind('contextmenu', function(e) {
// Check if onContextMenu() defined
var bShowContext = (!!hash[index].onContextMenu) ? hash[index].onContextMenu(e) : true;
currentTarget = e.target;
if (bShowContext) {
display(index, this, e, options);
return false;
}
});
createContexMenuFromNavigatorButtons函数的代码描述here我修改了
onContextMenu: function (e) {
var rowId = $(e.target).closest("tr.jqgrow").attr("id"), p = grid[0].p, i,
lastSelId;
if (rowId && getSelectedText() === '') {
...
return true;
} else {
return false; // no contex menu
}
}
使用getSelectedText() 并仅在未选择文本时创建上下文菜单。因此,只有在未选择文本时,您才会看到自定义上下文菜单,如果存在文本选择,则会看到标准上下文菜单(取决于 Web 浏览器):
更新:根据答案,我用additional information 修改了关于jquery.contextmenu.js 的错误报告。我希望 jquery.contextmenu.js 的主代码尽快更改,包含在 plugins 子目录中。
更新 2:如何查看 here 所有修复都已在 github 上的 jqGrid 主代码中,并包含在 jqGrid 4.3 中。
更新 3:如果您希望为所有 启用 <input type="text" ...>、<input type="textarea" ...> 和 <textarea ...> 元素提供标准上下文菜单,您只需稍作修改onContextMenu 回调中的代码。例如
onContextMenu: function (e) {
var p = grid[0].p, i, lastSelId,
$target = $(e.target),
rowId = $target.closest("tr.jqgrow").attr("id"),
isInput = $target.is(':text:enabled') ||
$target.is('input[type=textarea]:enabled') ||
$target.is('textarea:enabled');
if (rowId && !isInput && getSelectedText() === '') {
...
再查看一个demo,其中将通过双击激活内联编辑。