wdContextMenu获取被点击的元素ID

时间:2014-02-22 16:02:10

标签: javascript jquery

我使用此上下文菜单右键单击表:tr元素: http://www.downloadjavascripts.com/list/javasitelll99/Details.aspx

工作正常!我定义了一个动作处理函数。

var option = { width: 150, items: [
    { text: "Edit", icon: "public/images/sample-css/wi0126-16.gif", alias: "1-1", action: menuAction },
    { text: "Activate", icon: "public/images/sample-css/ac0036-16.gif", alias: "1-2", action: menuAction },
    //this is normal menu item, menuAction will be called if this item is clicked on
    { text: "Deactivate", icon: "public/images/sample-css/ei0021-16.gif", alias: "1-3", action: menuAction },
    //this is a split line
    { type: "splitLine" },
    //this is a parent item, which has some sub-menu items
    { text: "Delete", icon: "public/images/sample-css/ei0021-16.gif", alias: "1-3", action: menuAction },
    { type: "splitLine" },
    { text: "Item Four", icon: "public/images/sample-css/wi0124-16.gif", alias: "1-5", action: menuAction },
    { text: "Group Three", icon: "public/images/sample-css/wi0062-16.gif", alias: "1-6", type: "group", width: 180, items: [
        { text: "Item One", icon: "public/images/sample-css/wi0096-16.gif", alias: "4-1", action: menuAction },
        { text: "Item Two", icon: "public/images/sample-css/wi0122-16.gif", alias: "4-2", action: menuAction }
    ]
    }
]
};


function menuAction(){
    alert(this.data.alias);
}

但是,现在我想得到被点击的表的id:tr元素。 如何获得它?

2 个答案:

答案 0 :(得分:2)

var myElementId;

var option = { ...
    onShow: applyrule,
    onContextMenu: BeforeContextMenu
};

function menuAction() {
    alert(myElementId);
}

function applyrule(menu) {  
    myElementId = this.id
}

答案 1 :(得分:0)

假设tableId是你的表的id,这个函数可以帮助你:

function getIdOfRowByIndex(tableId, rowIndex) {
    return document.getElementById(tableId).rows[rowIndex].id;
}

如果你有一个tr元素,那么这个函数可以帮助你:

function getIdOfRow(trElement) {
    return trElement.id;
}

您可以使用jQuery来简化您的任务,但这些功能与技术无关,可以帮助那些不想/可以使用jQuery的人。