我有一個小問題,我的JQuery代碼。點擊行後,從其他行中刪除seletced類
我在HTML中創建了簡單的表格。
<table class="table">
<tr>
<th>Name</th>
<th>Surname</th>
<th></th>
</tr>
<tr>
<td>name 1</td>
<td>surname 1</td>
<td>actions</td>
</tr>
<tr>
<td>name 2</td>
<td>surname 3</td>
<td>actions</td>
</tr>
</table>
然後我想強調的黃色row
,當我點擊這個row
所以,當我點擊row
添加類「selected
」但是當我點擊另一個row
我不得不刪除「selected
」上一行的類。於是,我就在環刪除「selected
」類中創建JQuery的行動
$('tr').not(':first').click(function() {
var table = $(this).closest("table");
var rows = table.children("tr");
alert(rows.length);
rows.each(function() {
$(this).removeClass("selected");
});
$(this).addClass('selected');
});
我試圖找到最接近的表從這個點擊row
,並得到所有的孩子<tr>
, 旁邊並添加點擊row
這個類。
但總是alert(rows.length)
返回我0 row
S:<
請幫助我。
謝謝。
謝謝,它在現場爲少數表工作! –