Javascript 使用Jquery查找特定类的上一个div

Javascript 使用Jquery查找特定类的上一个div,javascript,jquery,Javascript,Jquery,我使用jquery,我想找到前面的div,它有“error”类 我的html代码: <table class="questions"> <tr> <td class="heading">1 <div class="error"></div> </td> <td colspan="4"> <textare

我使用jquery,我想找到前面的div,它有“error”类

我的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').prev("tr").next(".error")

怎么办?

假设您的
.error
div始终在同一个表行中:

$('#reponse137-3').closest('tr').find('.error:first');

假设您的
.error
div始终在同一个表行中:

$('#reponse137-3').closest('tr').find('.error:first');

您需要向上三次才能到达表,然后再查找错误

$('#reponse137-3').parent().parent().parent().find('.error');

第一个父级转到th,第二个转到tr,第三个转到table,然后在表中查找类名为的元素。error,您将得到它。

您需要向上三次才能到达table,然后查找error

$('#reponse137-3').parent().parent().parent().find('.error');

第一个父级转到th,第二个转到tr,第三个转到table,然后在表中查找元素的类名称。错误,您将得到它。

您忘记了选择器中的#。我忘记了它,因为我使用了jquery validator,并且直接引用了我的元素。很抱歉出现这个错误。您忘记了选择器中的#。我忘记了它,因为我使用了jquery验证器,并且直接引用了我的元素。很抱歉出现此错误。但是如果
tr
元素中有多个元素具有class
error
,则此错误不起作用@塞缪尔·凯勒里:这很有效。是each的子元素,但如果
tr
元素中有多个元素具有class
error
,则它不起作用@塞缪尔·凯勒里:这很有效。是每个的子项。查找不在
标签上,而是在
输入上。查找不在
标签上,而是在
输入上。