1
jQueryを使用して、前のh1
要素を選択して.show_more
に追加しようとしています。私はprev()
とclosest()
を使ってみましたが、いずれもうまくいかないようです。親のためのjQueryは前の要素を選択します(子要素ではない兄弟)
$(document).ready(function() {
$('.show_more').closest('h1').addClass('gummybear');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<h1>Test</h1>
<p>I am a paragraph</p>
<div class="show_more">Show More</div>
element.parent()。find()を使用すると、コードに示されているレベルと同じレベルにある場合にこの問題を解決できます。 –