closest()
は、要素自体をテストし、DOMツリーの祖先を走査することによってセレクタに一致する最初の要素を取得します。しかし、私が知りたいと思うのは、結果を得るための方法はありますか?ありがとう。jqueryに最も近い除外自己
例を挙げておきます。それを確認してください。
<div id='div1' class="container">
<div id="div2" class="container">
<!--many nested div.container as it directly or indirectly children -->
</div>
<div id="div3" class="container">
<!--many nested div.container as it directly or indirectly children -->
</div>
</div>
$(function() {
$('div.container').on('mouseover', function(e){
//I only want to select the first parent with the class container.
//closest include the this element.
console.log($(this).closest('.container').attr('id'));
});
});
もっと詳しく説明できますか?コードスニペットを追加するとより便利になります... – Reigel
'.parents()'が役に立ちます。 –
@Reigelもう一度レビューしていただけますか?私は更新しました。ありがとう。 –