javascript jQuery 上下文菜单被点击

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4725978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 14:41:39  来源:igfitidea点击:

jQuery context menu get clicked item

javascriptjqueryjquery-selectorscontextmenu

提问by Upvote

I am using jQuery context menu plugin by Chris Domiganto appy a context menu. This is how I do it:

我正在使用Chris Domigan 的 jQuery 上下文菜单插件来应用上下文菜单。这就是我的做法:

$('#contacts tbody tr').contextMenu('myMenu1', {
    bindings: {
        'copy': function(t) {
             alert('Trigger was '+t.id+'\nAction was Copy');
         },

        'delete': function(t) {
             alert('Trigger was '+t.id+'\nAction was Delete');
        }
    },             
});

My question is, how do I get the content of the clicked tr item? I tried with

我的问题是,如何获取点击的 tr 项目的内容?我试过

$(t.target).html()

but it returns null. Any idea?

但它返回null。任何的想法?

EDIT:here is the example http://jsfiddle.net/gqhRV/

编辑:这是示例http://jsfiddle.net/gqhRV/

回答by Ramon Lucas

I think is this what you want:

我想这就是你想要的:

<script type="text/javascript"> 
$(function(){
$.contextMenu({
    selector: '.flexme1 tbody tr', 
    callback: function(key, options) {
        alert("Clicked on " + key + " on element " + options.$trigger.attr('id').substr(3));            
    },
    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"}
    }
});

$('.flexme1 tbody tr').on('click', function(e){
    console.log('clicked', this);
})
});
</script>

It's integrated with the Flexigrid... works fine for me...

它与 Flexigrid 集成在一起......对我来说很好用......

And obviously i have some extra options.

显然我有一些额外的选择。

回答by hunter

not familiar with the plugin, but from the looks of it you should be able to write:

不熟悉该插件,但从它的外观来看,您应该能够编写:

$("#" + t.id).html();

but in the case of most jQuery plugins you should be able to do this:

但在大多数 jQuery 插件的情况下,你应该能够做到这一点:

$(this).html();

from within the context of the 'copy': function(t) {and 'delete': function(t) {

从的范围内'copy': function(t) {'delete': function(t) {



$('#contacts tbody tr').contextMenu('myMenu1', {
    bindings: {
        'open': function(t) { ShowAction(t, "Open"); },
        'email': function(t) { ShowAction(t, "Email"); },
        'save': function(t) { ShowAction(t, "Save"); },
        'delete': function(t) { ShowAction(t, "Delete"); }
    }
});

function ShowAction(t, a) {
    alert('Trigger was ' + t.id + '\nAction was ' + a + "\nHtml is " + $(t).html());
}

Here's a working example: http://jsfiddle.net/dNUgg/

这是一个工作示例:http: //jsfiddle.net/dNUgg/

I'm guessing your <tr>tags do not have an idattribute

我猜你的<tr>标签没有id属性



Even when the <tr>does not have an ID this still works: http://jsfiddle.net/dNUgg/1/

即使<tr>没有 ID 这仍然有效:http: //jsfiddle.net/dNUgg/1/



 alert('content is ' + $(t).text() + '\nAction was Delete');

updated your jsfiddle: http://jsfiddle.net/gqhRV/1/

更新了你的 jsfiddle:http: //jsfiddle.net/gqhRV/1/

you were doing $(t.target).text()when you should be doing $(t).text()

$(t.target).text()应该做的时候你在做$(t).text()