jQueryのnextAllで現在要素タグ以降の兄弟要素タグの背景色を全て変更してみます。
タグとCSSで変更するのが面倒な時に使用できます。
div要素の背景を全てグレー(#cccccc)にCSSを記述
※必要に応じて書き換えてください。
<style type="text/css"> body{ font-family:Verdana,"Hiragino Kaku Gothic Pro","ヒラギノ角ゴ Pro W6",Osaka,"MS Pゴシック",Arial,sans-serif; padding: 0; margin: 0; overflow-x: hidden; line-height: 1.8em; text-align: center; } h1{ font-size:16px; text-align:center; font-weight:normal; padding:10px 0; position: relative; } div{ display: inline-block; width: 50px; height: 50px; margin: 0 10px; background-color: #cccccc; } </style>
HTML記述
<h1>1番目のdiv要素タグ(class="one")以降の兄弟要素タグの背景を全て赤色に変更してます。</h1> <div class="one"></div> <div></div> <div></div> <div></div>
nextAllで兄弟要素の背景色を変更するJavaScriptの記述
※class=oneのdivタグ以降の背景色を全て赤色(#ff0000)に変更してます。
<script src="./jquery-2.2.0.min.js" type="text/javascript"></script> <script type="text/javascript"> $('.one').nextAll().css({'background-color':'#ff0000'}); </script>
デモページ
nextAllを使って、現在要素タグ以降の兄弟要素タグの背景色を全て変更するデモ
※流用される場合は自己責任でお願いします。