jquery中的愚蠢错误

时间:2014-10-07 07:56:32

标签: jquery jquery-selectors

我有一个jquery代码

$(function(){
    $.contextMenu({
        selector: '.context-menu-icon', 
        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)
            return {
                callback: function(key, options) 
                {
                    if(key=='delete')
                        $(this).remove();
                },
                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"},
                    "sep1": "---------",
                    "quit": {name: "Quit", icon: "quit"}
                }
            };
        }
    });
});

在线,    selector: '.context-menu-icon',
我想把两个类作为选择器

所以我就这样做了

selector: {'.context-menu-icon','context-menu-text'},

但不知道为什么,它不起作用..

任何建议

2 个答案:

答案 0 :(得分:3)

假设您使用this插件,您是否尝试过修改代码,如:

$(function(){
    $.contextMenu({
        selector: '.context-menu-icon, .context-menu-text', 
        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)
            return {
                callback: function(key, options) 
                {
                    if(key=='delete')
                        $(this).remove();
                },
                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"},
                    "sep1": "---------",
                    "quit": {name: "Quit", icon: "quit"}
                }
            };
        }
    });
});

注意:这是未经测试的,但根据文档,我认为它应该有效。

答案 1 :(得分:2)

该插件中的selector属性是一个字符串,但您正在尝试使用对象。

只需使用逗号(请参阅multiple selector)在选择器中将它们分开:

selector: '.context-menu-icon, .context-menu-text'