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'));"
精彩评论