开发者

How do I get the row (and its index) in an html table from a button click inside a cell in that row

The question is in 开发者_如何学JAVAthe subject line...


$("yourtableselector td").click(function() {
    alert($(this).parents("tr").get(0).rowIndex);
});


You have to go back up through the DOM until you find the <tr> element.

function onclick_handler( event ) {
    var element = event.target;
    while( element.tagName != 'TR' && element.parentNode ) {
        element = element.parentNode;
    }
    return element.rowIndex;
}


onclick="alert(this.parentNode.rowIndex);"

Unless you have a tbody type tag, then use parentNode twice. Or in jquery:

onclick="alert($(this).closest('tr').attr('rowIndex'));"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜