Rok*_*kal 3 jquery jquery-selectors
我试图找到最接近的tr,它也匹配特定的类名.选择器不返回任何匹配项.我不明白的是什么?
例如:
<table>
<tbody class="category" id="cat-1">
<tr class="category-head">
<th>Category 1</th>
</tr>
<tr class="category-item">
<th>Item 1</th>
</tr>
<tr>
<td>This is a description of category item 1</td>
</tr>
<tr>
<td><input type="text" name="c1_i1_input" id="c1_i1_input" class="category-item-input" /></td>
</tr>
<tr class="category-item">
<th>Item 2</th>
</tr>
<tr>
<td>This is a description of category item 2</td>
</tr>
<tr>
<td><input type="text" name="c1_i2_input" id="c1_i2_input" class="category-item-input" /></td>
</tr>
</tbody>
</table>
<script type="text/javascript">
$(document).ready(function () {
$(".category-item-input").change(function () {
//this works, returning the closest tr
alert($(this).closest("tr").html());
//this returns null?
alert($(this).closest("tr.category-item").html());
});
});</script>
Run Code Online (Sandbox Code Playgroud)
最近找到最近的祖先元素,而不是DOM中最近的元素.在您的情况下,tr包含输入没有类,因此选择器失败.您可能想要找到最接近的tr,然后找到tr该类的前一个兄弟.
$(this).closest('tr').prevAll('tr.category-item:last');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4951 次 |
| 最近记录: |