看看这个:
$(document).ready(function(){
$('.TreeTable tr').click(function(e){
var cell = $(e.target).get(0); // This is the TD you clicked
var tr = $(this); // This is the TR you clicked
$('#out').empty();
$('td', tr).each(function(i, td){
$('#out').html(
$('#out').html()
+'<br>'
+i+': '+$(td).text()
+ (td===cell?' [clicked]':'') );
});
});
});
这是工作代码:http:
//jsfiddle.net/VbA9D/
如果您在可能单击的表格单元格中有其他 HTML 元素,则以下示例将更好地工作:
$(document).ready(function(){
$('.TreeTable tr').click(function(e){
var cell = $(e.target).get(0); // This is the TD you clicked
if(cell.nodeName != 'TD')
cell = $(cell).closest('td').get(0);
var tr = $(this); // This is the TR you clicked
$('#out').empty();
$('td', tr).each(function(i, td){
$('#out').html(
$('#out').html()
+'<br>'
+i+': '+$(td).text()
+ (td===cell?' [clicked]':'') );
});
});
});
这是您可以测试的代码:
http://jsfiddle.net/7PWu5/