开发者

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

开发者 https://www.devze.com 2022-12-14 20:10 出处:网络
The question is in 开发者_如何学JAVAthe subject line...$(\"yourtableselector td\").click(function() {

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

精彩评论

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