一个很好的选择器来删除这个可怕的代码

Kie*_*ron 0 jquery jquery-selectors

任何人都可以提供一个选择器来消除这些可怕的代码块:

// img is the image element...
var descriptionContent = $(".descriptionContent", img.parent("td").parent("tr").next("tr"));
Run Code Online (Sandbox Code Playgroud)

html看起来像这样:

<table>
    <tr>
        <td><img /></td>
    </tr>
    <tr>
        <td><div class="descriptionContent" /></td>
    </tr>
    <!-- Repeat n times -->
</table>
Run Code Online (Sandbox Code Playgroud)

鉴于用户点击了img,我需要获得下一个(也是下一个).descriptionContent元素.

谢谢,
K

Wal*_*t W 5

由于您担心将图像包装在某物中,请尝试以下操作:

var descriptionContent = $(".descriptionContent", img.closest("tr").next("tr"));
Run Code Online (Sandbox Code Playgroud)

最近的命令找到与给定选择器匹配的最近祖先. 看这里.