我使用jquery,我想找到前一个具有“error”类的div
我的html代码:
<table class="questions">
<tr>
<td class="heading">1
<div class="error"></div>
</td>
<td colspan="4">
<textarea class="textcontrol required" name='questionT136'>
</textarea>
</td>
</tr>
<tr>
<td></td>
<th><label for="reponse137-3">Très satisfaisant</label></th>
<th><label for="reponse137-4">Satisfaisant</label></th>
<th><label for="reponse137-5">Insatisfaisant</label></th>
</tr>
<tr class="q">
<td class="heading">
2
<div class="error"></div>
</td>
<td class="questionradio"><input class="required" id='reponse137-3'
name='questionN137' type='radio' value='3'></td>
<td class="questionradio"><input class="required" id='reponse137-4'
name='questionN137' type='radio' value='4'></td>
</tr>
</table>
例如,从reponse137-3,我想更改之前的 class="error"的值。我已经准备好尝试使用以下方式访问该对象:
$('#reponse137-3').prev(".error")
但它不起作用。我想因为它没有同一个 parent ,所以我尝试了:
$('#reponse137-3').prev("tr").next(".error")
我该怎么办?
最佳答案
假设您的 .error
div 始终位于同一表格行中:
$('#reponse137-3').closest('tr').find('.error:first');
关于javascript - 使用 Jquery 查找特定类的上一个 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12264025/