jquery - 使用 jQuery contextMenu 插件获取打开上下文菜单的单击元素的 ID

标签 jquery jquery-plugins contextmenu

我使用jQuery contextMenu来自 Rodney Rehm 的 SVG 图形。对于基本使用来说它工作得很好。

但我需要获取触发上下文菜单的 SVG 元素的 ID(或任何其他属性),以便在上下文菜单的项目列表中使用它来获取动态项目名称。

我与 Simple Context Menu 合作演示,现在想要根据单击的 SVG 元素的 ID 将这些静态菜单项替换为动态菜单项。

最佳答案

这可能对您有帮助:http://medialize.github.com/jQuery-contextMenu/demo/dynamic-create.html
这是一些示例代码:

$(function(){
    $.contextMenu({
        selector: 'my-selector-here', 
        build: function($trigger, e) {
            // this callback is executed every time the menu is to be shown
            // its results are destroyed every time the menu is hidden
            // e is the original contextmenu event, containing e.pageX and e.pageY (amongst other data)
            // $trigger is the element that was rightclicked on - get its id here
            var id = $trigger.getTheIDSomehow()
            // build the menu items
            if (id == 1) {
              menuItems = {...}
            else if (id == 2)
              menuItems = {...}
            return {
                callback: function(key, options) {
                    // this is called when one of the contextmenu options is clicked
                },
                items: menuItems
            };
        }
    });
});

关于jquery - 使用 jQuery contextMenu 插件获取打开上下文菜单的单击元素的 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11950811/

相关文章:

php - 搜索正文内容,用弹出窗口突出显示

javascript - 固定位置导航栏不起作用

jquery - 使用 jQuery 变形图像

contextmenu - 如何自定义Slack Windows客户端的上下文菜单?

windows - 如何获得上下文菜单选择的路径?

javascript - 在 jquery 中翻转多行文本的最快方法

javascript - 使用循环将两个字符串字符排列成一个字符串

javascript - 使用 Qunit 测试返回的函数

c# - 如何将 ContextMenu 放入 TabPage 的页眉

javascript - 是否有可能用 js/dojo/jquery 复制一个表单?