0

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

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

html 看起来像这样:

<table>
    <tr>
        <td><img /></td>
    </tr>
    <tr>
        <td><div class="descriptionContent" /></td>
    </tr>
    <!-- Repeat n times -->
</table>

鉴于用户点击了 img,我需要获取下一个(并且只有下一个).descriptionContent元素。

谢谢,

4

2 回答 2

5

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

var descriptionContent = $(".descriptionContent", img.closest("tr").next("tr"));

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

于 2009-09-09T15:03:30.587 回答
0

我想你会听到很多关于总体结构方式的信息。如果你消除桌子,很多这个问题就会消失。

就像是:

<div class="imgAndContent">
  <img src="blah.jpg">
  <span class="someDescription">Description.</span> <!-- or div, you choose based on your need -->
</div>

然后你可以简单地做......

var descriptionContent = $(this).next();

当用户点击...

于 2009-09-09T15:03:32.243 回答