それを行うための最も簡単な方法は次のとおりです。
// jQuery:
$("#table-one tr").bind("click", function() {
alert(this.id);
});
bind
がちょうどよく、ハンドラ:)
作業をサンプルにイベントをバインドするjQueryの方法であり、 jQueryフレームワークを使用します。
// Your function
function MyFunc(rowId){
alert(rowId);
}
// Binde click event to all rows with class "ClockableRow"
// within table with class "clickableTable"
$(".clickableTable .clickableRow").click(function(){
// Call you function and pass clicked row ID there
MyFunc($(this).attr("id"));
})
あなたのテーブルには、次のようになりshoul:
<table class="clickableTable">
<tr id="row1" class="clickableRow"><td>row 1</td></tr>
<tr id="row2" class="clickableRow"><td>row 2</td></tr>
<tr id="row3" class="clickableRow"><td>row 3</td></tr>
<tr id="row4" class="clickableRow"><td>row 4</td></tr>
<tr id="row5" class="clickableRow"><td>row 5</td></tr>
</table>
example
はあなたがJavaScriptライブラリを([MooToolsは](http://mootools.net/)、[プロトタイプ](使用していますhttp://www.prototypejs.org/)、[jQuery](http://www.jquery.com)、[Glow](http://www.bbc.co.uk/glow/)、[Scriptaculous] (http://script.aculo.us/)...)、または 'plain'-vanilla JavaScript? –
2つの小さな発言:1)IDは数字で始めることはできません。 idとして "1"ではなく "row1"のようなものを使用してください。 B)多くの場合(ほとんどではないが)、IDの代わりに行への参照を関数に渡す方が合理的です。あなたの仕事で何をしていますか?これに答えると、より具体的な答えを得ることができます。 – RoToRa