jQuery在表上使用上下文菜单

时间:2013-05-22 07:27:55

标签: jquery jquery-datatables

我正在使用数据表,对于任何操作,我想要具有on()函数的上下文菜单。我可以使用.live()函数,但这种方法升级到新版本的jquery并使用.on()下面的代码不起作用,怎么能帮我改变呢?

jquery LIVE功能(使用它没问题):

$('#showCategories tbody tr').live('contextmenu', function (event) {
    var nTds_showCategories = $('td', this);
    $(oTable_categories.fnSettings().aoData).each(function (){$(this.nTr).removeClass('row_selected');});
    $(event.target.parentNode).addClass('row_selected');
    $('.showCategoriesMenus').css({'top' : event.pageY ,'left' : event.pageX-150});
    $('.showCategoriesMenus').show();
    iId_categories = $(nTds_showCategories[0]).text() ;
    event.returnValue= false;
    return false;
});

此方法不起作用:

$("#showCategories").on('contextmenu', '#showCategories tbody tr', function(event){
    var nTds_showCategories = $('td', this);
    $(oTable_categories.fnSettings().aoData).each(function (){$(this.nTr).removeClass('row_selected');});
    $(event.target.parentNode).addClass('row_selected');
    $('.showCategoriesMenus').css({'top' : event.pageY ,'left' : event.pageX-150});
    $('.showCategoriesMenus').show();
    iId_categories = $(nTds_showCategories[0]).text() ;
    event.returnValue= false;
    return false;
});

HTML / PHP:

我正在为所有数据表使用display

echo"
<div id='pane_category' class='scroll-pane' style='height: 364px;'>
    <ul class='styledlist' >
        <table cellpading=0 cellspacing=0 class='display' id='showCategories'>
            <thead>
                <tr>
                    <th style='width:5%;height:12px!important;'>".$popular_messages['id']."</th>
                    <th style='width:95%;'>".$admin_contents['title']."</th>
                </tr>
            <thead>
            <tbody style='line-height:25px;'>
            </tbody>
        </table>
    <ul>
</div>";

1 个答案:

答案 0 :(得分:2)

删除第二个参数上的#showCategories ..它应该是这样的:

$("#showCategories").on('contextmenu', 'tbody tr', function(event){
    ....
});