gpt4 book ai didi

jquery - 单击 jQuery contextMenu 的表中单元格的值

转载 作者:行者123 更新时间:2023-12-01 03:09:42 26 4
gpt4 key购买 nike

考虑以下jsfiddle .

它使用 jQuery contextMenu显示添加到表格主体的右键单击上下文菜单。

<table border="1">
<tbody class="context-menu-one">
<tr>
<td>R1C1</td>
<td>R1C2</td>
</tr>
<tr>
<td>R2C1</td>
<td>R1C2</td>
</tr>
</tbody>
</table>

这是 jQuery:

$(function() {
$.contextMenu({
selector: '.context-menu-one',
callback: function(key, options) {
var clickedKey= key;
//How to get the Value of the clicked cell here ?
var m = $(options.$trigger).text();
window.console && console.log(m) || alert(m);
},
items: {
"edit": {name: "Edit", icon: "edit"},
"delete": {name: "Delete", icon: "delete"},
}
});

$('.context-menu-one').on('click', function(e){
console.log('clicked', this);
})
});

如何获取调用上下文菜单的单元格的值?

例如,通过单击表格的第 1 行、1 列来调用上下文菜单应该给我值 R1C1

最佳答案

您需要使选择器:选项选择表格单元格。然后回调中的 this 将是您单击的单元格。

$(function() {
$(".context-menu-one").contextMenu({
selector: 'td',
callback: function(key, options) {
var content = $(this).text();
alert("You clicked on: " + content);
},
items: {
"edit": {name: "Edit", icon: "edit"},
"delete": {name: "Delete", icon: "delete"},
}
});
});

DEMO

关于jquery - 单击 jQuery contextMenu 的表中单元格的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34366090/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com