この恐ろしいコードを削除する素晴らしいセレクター
-
05-07-2019 - |
質問
この恐ろしいコードの塊をなくすセレクタを誰でも提供できますか:
// 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
要素を取得する必要があります。
ありがとう、
K
解決
画像を何かに包むのが心配なので、これを試してください:
var descriptionContent = $(".descriptionContent", img.closest("tr").next("tr"));
最も近いコマンドは、指定されたセレクターに一致する最も近い先祖を見つけます。 こちらをご覧ください。
他のヒント
これが一般的にどのように構成されているかについて、あなたはよく耳にするでしょう。テーブルを削除すると、この問題の多くはなくなります。
次のようなもの:
<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();
ユーザーがクリックしたとき...
所属していません StackOverflow