テーブル、このタグの上とか、最初以外とか
結果はこんなカンジ
$("table.map th, table.map td").click(function() { var index = $(this).index(); //一番左の列は禁止、 if(index>0){ //初期化 $(this).parents().find('tr').each(function() { $('td, th', this).removeClass('click'); }); //最初の行以外 $(this).closest('table').find('tr').not(":first").each(function() { $('td, th', this).eq(index).addClass('click'); }); } });
紆余曲折したコード・・・地道に辿れるけど、めんどくさい。
$(this).addClass('click'); var tName = $(this).get(0).tagName; // TDなら次のTHをハイライト if(tName=="TD"){ $('th', $(this).parent().nextAll()).eq(index).addClass('click'); } // THなら前のTDをハイライト if(tName=="TH"){ $('td', $(this).parent().prevAll(index)).eq(index-1).addClass('click'); }