简体   繁体   English

jQuery上下文菜单获取被点击的项目

[英]jQuery context menu get clicked item

I am using jQuery context menu plugin by Chris Domigan to appy a context menu. 我正在使用Chris Domigan的jQuery上下文菜单插件来应用上下文菜单。 This is how I do it: 我是这样做的:

$('#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? 我的问题是,如何获取所点击的tr项目的内容? I tried with 我试过了

$(t.target).html()

but it returns null . 但它返回null Any idea? 任何想法?

EDIT: here is the example http://jsfiddle.net/gqhRV/ 编辑:这是http://jsfiddle.net/gqhRV/的例子

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. 显然我有一些额外的选择。

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) {的上下文'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 id attribute 我猜你的<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).text()时,你正在做$(t.target).text() $(t).text()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM