【发布时间】:2025-12-30 19:20:08
【问题描述】:
我无法使上下文菜单正常工作。我想要的是当我单击任何行时,它会提醒我其中的第一个 td 文本。
这是我初始化数据表的代码:
var init_item_seized_tbl = function init_item_seized_tbl(){
$('#item_seized_tbl').DataTable({
"autoWidth": false,
"aoColumnDefs": [
{ "bSortable": false, "aTargets": [ 4 ] },
{ "sWidth": "20%", "aTargets": [ 0 ] },
{ "sWidth": "40%", "aTargets": [ 1 ] },
{ "sWidth": "10%", "aTargets": [ 2 ] },
{ "sWidth": "20%", "aTargets": [ 3 ] },
{ "sWidth": "10%", "aTargets": [ 3 ] },
],
"fnCreatedRow" : function( nRow, aData, iDataIndex ){
$(nRow).addClass('item-context');
},
"fnRowCallback" : function( nRow, aData, iDisplayIndex, iDisplayIndexFull){
console.log('fnRowCallback');
$('table#item_seized_tbl tr').on('mouseenter', function () {
$(this).contextMenu({
selector: '.item-context',
callback: function(key, options) {
//var m = "clicked: " + key;
//window.console && console.log(m) || alert(m);
},
items: {
"edit": {name: "Edit", icon: "edit"},
"cut": {name: "Cut", icon: "cut"},
"copy": {name: "Copy", icon: "copy"},
"paste": {name: "Paste", icon: "paste"},
"delete": {name: "Delete", icon: "delete"},
}
},
function (action, el, pos) {
alert(
'Action: ' + action + '\n\n' +
'Element ID: ' + $(el).attr('id') + '\n\n' +
'X: ' + pos.x + ' Y: ' + pos.y + ' (relative to element)\n\n' +
'X: ' + pos.docX + ' Y: ' + pos.docY + ' (relative to document)\n\n'
);
}
);
});
}
});
}
问题是上下文菜单没有出现。
我尝试了另一种方法,将上下文菜单的初始化分开。但我不知道如何处理这些事件并连续提醒我第一个 td。
$.contextMenu({
selector: '.item-context',
callback: function(key, options) {
var m = "clicked: " + key;
window.console && console.log(m) || alert(m);
},
items: {
"edit": {name: "Edit", icon: "edit"},
"cut": {name: "Cut", icon: "cut"},
"copy": {name: "Copy", icon: "copy"},
"paste": {name: "Paste", icon: "paste"},
"delete": {name: "Delete", icon: "delete"},
}
});
您的回复将不胜感激。谢谢!
【问题讨论】:
标签: javascript jquery contextmenu jquery-datatables