<html> <head> <script> function test(obj){ var index = $('td').index(obj);// var index $('td').index($(obj));也可以 $('table').children('tr').each(function(){ $(this).children('td').each(function(){ $(this).eq(index).css(className); }); }); } </script> </head> <body> <table>
方法做了修改 var last_index=''; function test(obj){ var index = $('td').index(obj);// var index $('td').index($(obj));也可以 $('#tab tr').each(function(){ $(this).children('td').eq(index).css('background','red'); if(last_index!=''){ $(this).children('td').eq(last_index).css('background',''); } }); last_index=index; }
<head>
<script>
function test(obj){ var index = $('td').index(obj);// var index $('td').index($(obj));也可以
$('table').children('tr').each(function(){
$(this).children('td').each(function(){
$(this).eq(index).css(className);
});
});
}
</script>
</head>
<body>
<table>
<tr>
<td onclick="test(this)">1</td>
<td onclick="test(this)">2</td>
<td onclick="test(this)">3</td>
<td onclick="test(this)">4</td>
</tr> </table>
</body>呵呵 ,一个一个敲上去的,你自己简化下吧
var last_index='';
function test(obj){
var index = $('td').index(obj);// var index $('td').index($(obj));也可以
$('#tab tr').each(function(){
$(this).children('td').eq(index).css('background','red');
if(last_index!=''){
$(this).children('td').eq(last_index).css('background','');
}
});
last_index=index;
}