最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery context menu get clicked item - Stack Overflow

programmeradmin0浏览0评论

I am using jQuery context menu plugin by Chris Domigan to appy a context menu. 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? I tried with

$(t.target).html()

but it returns null. Any idea?

EDIT: here is the example /

I am using jQuery context menu plugin by Chris Domigan to appy a context menu. 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? I tried with

$(t.target).html()

but it returns null. Any idea?

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

Share Improve this question edited Jan 18, 2011 at 16:08 DarkLeafyGreen asked Jan 18, 2011 at 15:41 DarkLeafyGreenDarkLeafyGreen 70.4k135 gold badges390 silver badges614 bronze badges 7
  • 1 thats really vague please include your html, even better create a jsFiddle! – Amjad Masad Commented Jan 18, 2011 at 15:56
  • show some HTML, I'm guessing you're doing something abnormal – hunter Commented Jan 18, 2011 at 16:07
  • here is the example code jsfiddle.net/gqhRV – DarkLeafyGreen Commented Jan 18, 2011 at 16:08
  • when I add an id to your tr tag, that's what value comes back in the copy popup. not quite what the documentation suggests. – Tom B Commented Jan 18, 2011 at 16:20
  • why your link(plugin) is redirecting to a kitchen's site? – Chandan Kumar Thakur Commented Jul 13, 2019 at 12:17
 |  Show 2 more comments

2 Answers 2

Reset to default 9

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...

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:

$(this).html();

from within the context of the 'copy': function(t) { and '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/

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


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


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

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

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

发布评论

评论列表(0)

  1. 暂无评论