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

cbl*_*cbl 7 jquery contextmenu jquery-plugins

我在SVG图形中使用Rodney Rehm 的jQuery contextMenu.它适用于基本用法.

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

我使用简单上下文菜单演示,现在想要用动态菜单替换这些静态菜单项,具体取决于单击的SVG元素的ID.

Abr*_*ham 14

这可能对您有所帮助: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
            };
        }
    });
});
Run Code Online (Sandbox Code Playgroud)


g.a*_*g.a 8

当我使用静态菜单时,我得到这样的id:

...
callback: function (key, options) {
    id = options.$trigger.attr("id");
    ...
},
...
Run Code Online (Sandbox Code Playgroud)

也许$ trigger.attr("id")可能适合你.


Rah*_*hul 8

您只需要检查右键单击的元素是:::

$.contextMenu({
            selector: 'tr',
            callback: function (key, options) {
                var m = "clicked: " + key;
                if (key == "Clone") 
               {
                    Your_Function($(this).attr('id'));
               }

            },
            items: {
                "Clone": { name: "Clone" },
                }
            }); 
Run Code Online (Sandbox Code Playgroud)