初心者向けjQuery入門

初心者向けjQuery入門講座です

未分類

jQuery nth-child偶数と奇数と二番目指定する方法

投稿日:2017年8月6日 更新日:

jQuery nth-child偶数と奇数nth-last-childとnth first child

jQuery nth-child

nth-childは何番目の要素を指定するセレクタです。

nth-child 何番目の要素

二番目の要素を指定しました。

nth-child(2)
jQuery
$(“h3:nth-child(2)”)

サンプル

<html>
<head>
<script src=”jquery-3.2.1.js”></script>

<script>
$(document).ready(function(){

$(“h3:nth-child(2)”).css({“font-size”:”72px”,”font-style”:”italic”})

});
</script>
<title>

</title>
</head>
<div>
<h3>red</h3>
<h3>Green</h3>
<h3>yellow</h3>
<h3>Blue</h3>
</div>
</body>
</html>

nth-child 偶数

偶数を指定するにはevenを指定する方法があります。
nth-child(even)
jQuery
$(“h3:nth-child(even)”)

サンプル

<html>
<head>
<script src=”jquery-3.2.1.js”></script>

<script>
$(document).ready(function(){

$(“h3:nth-child(even)”).css({“font-size”:”72px”,”font-style”:”italic”})

});
</script>
<title>

</title>
</head>
<div>
<h3>red</h3>
<h3>Green</h3>
<h3>yellow</h3>
<h3>Blue</h3>
</div>
</body>
</html>

nth-child(2n)
jQuery
$(“h3:nth-child(2n)”)
2nは2の倍数だからです。

サンプル

<html>
<head>
<script src=”jquery-3.2.1.js”></script>

<script>
$(document).ready(function(){

$(“h3:nth-child(2n)”).css({“font-size”:”72px”,”font-style”:”italic”})

});
</script>
<title>

</title>
</head>
<div>
<h3>red</h3>
<h3>Green</h3>
<h3>yellow</h3>
<h3>Blue</h3>
</div>
</body>
</html>

nth-child 奇数

奇数を使用するにはoddを指定します。

nth-child(odd)
jQuery
$(“h3:nth-child(odd)”)

サンプル

<html>
<head>
<script src=”jquery-3.2.1.js”></script>

<script>
$(document).ready(function(){

$(“h3:nth-child(odd)”).css({“font-size”:”72px”,”font-style”:”italic”})

});
</script>
<title>

</title>
</head>
<div>
<h3>red</h3>
<h3>Green</h3>
<h3>yellow</h3>
<h3>Blue</h3>
</div>
</body>
</html>

2n+1で奇数を指定できます。

nth-child(2n+1)
jQuery
$(“h3:nth-child(2n+1)”)

サンプル

<html>
<head>
<script src=”jquery-3.2.1.js”></script>

<script>
$(document).ready(function(){

$(“h3:nth-child(2n+1)”).css({“font-size”:”72px”,”font-style”:”italic”})

});
</script>
<title>

</title>
</head>
<div>
<h3>red</h3>
<h3>Green</h3>
<h3>yellow</h3>
<h3>Blue</h3>
</div>
</body>
</html>

nth-child 以降を指定する

サンプルは2番目以降を指定してます。

nth-child(n+2)
jQuery
$(“h3:nth-child(n+2)”)

三番目以降ならn+3ですね。

サンプル

<html>
<head>
<script src=”jquery-3.2.1.js”></script>

<script>
$(document).ready(function(){

$(“h3:nth-child(n+2)”).css({“font-size”:”72px”,”font-style”:”italic”})

});
</script>
<title>

</title>
</head>
<div>
<h3>red</h3>
<h3>Green</h3>
<h3>yellow</h3>
<h3>Blue</h3>
</div>
</body>
</html>

 

スポンサードリンク




スポンサードリンク




-未分類
-, , ,

執筆者:


comment

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

関連記事

no image

jQuery beforeとafterの違いと要素の追加

jQuery beforeとafterの違いと要素の追加 jQuery beforeとafterの違い beforeとafterは基本的な動作は似てます。 共通してるのは要素を追加することです。 違う …

no image

jQuery addclassクラスを追加する方法

jQuery addclassクラスを追加する方法 jQuery addclassクラスを追加する方法 addclassクラスは要素にクラスを設定できます。 どういうことかというこクラスのタブにhtm …

no image

jQuery prevALLとnextALL前と後の兄弟要素を全て取得する

jQuery prevALLとnextALL前と後の兄弟要素を全て取得する jQuery prevALL前の兄弟要素を全て取得する prevALLメソッドは指定した前の兄弟要素を全て取得します。 サン …

no image

jQuery first-childとlast-childの勘違い

jQuery first-childとlast-child jQuery first-child first-childは一番最初の子要素を指定します。 $(“要素:first-child …

no image

jQuery slideUpとslideDownとslideToggle

jQuery slideUpとslideDownとslideToggle jQuery slideUp slideUpはスライドをアップします。 要素が上に上がっていき非表示になります。 サンプル $ …