Javascript 选择表行的更好方法?
我有以下(简化的)表格结构:Javascript 选择表行的更好方法?,javascript,jquery,Javascript,Jquery,我有以下(简化的)表格结构: <table class="form-table"> <tbody> <tr> <tr><--Need to select and add class on this guy based on h3 class "check"--> <th scope="row"> <h3 class="check">Default Checbox:</h3> </th> &
<table class="form-table">
<tbody>
<tr>
<tr><--Need to select and add class on this guy based on h3 class "check"-->
<th scope="row">
<h3 class="check">Default Checbox:</h3>
</th>
<td>
</tr>
</tbody>
</table>
但问题是,为了做出选择,我给了一个不必要的类“酷”
<table class="form-table">
<tbody>
<tr>
<tr>
<tr class="until"><--Class successfully added but..-->
<th class="cool" scope="row"><--An extra Class in order to select "table row"-->
<h3 class="check">Default Checbox:</h3>
</th>
<td>
</tr>
</tbody>
</table>
默认复选框:
有没有更好的方法来实现这一点(不添加不必要的类) 您可以使用
或
@佩德罗斯特拉达在打给你我知道这很简单:=)你让我开心。THX.
'tbody'
选择器是多余的,因为每个TR都是tbody的子项(无论标记是否包含在标记中)。
<table class="form-table">
<tbody>
<tr>
<tr>
<tr class="until"><--Class successfully added but..-->
<th class="cool" scope="row"><--An extra Class in order to select "table row"-->
<h3 class="check">Default Checbox:</h3>
</th>
<td>
</tr>
</tbody>
</table>
$('tbody tr').has('h3.check').addClass('cool');
$('tbody tr:(h3.check)').addClass('cool');