初心者向け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 first-childとlast-childの勘違い

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

no image

jQuery チルダ~の間接セレクタと兄弟セレクタ

jQuery チルダ~の間接セレクタと兄弟セレクタ jQuery チルダ~の間接セレクタと兄弟セレクタ 間接セレクタはチルダを使用します。 兄弟とは親が同じだということですね。 動作は指定した兄弟の以 …

no image

jQuery:notセレクタの使い方

jQuery jQuery:notセレクタの使い方 jQuery jQuery:notセレクタの使い方 :notセレクタは条件を指定した以外の要素を指定します。 :not(h2) 使用方法はセレクタに …

no image

jQuery showとhideで要素を表示と非表示する方法

jQuery showとhideで要素を表示と非表示する方法 jQuery show 要素を表示する方法 showの英語の意味は見せるです。 サンプル $(“セレクタ”).sh …

no image

jQuery widthとheight要素の横と縦を設定する

jQuery widthとheight要素の横と縦を設定する jQuery width 要素のサイズの横幅を設定するにはwidthメソッドを使用します。 $(“セレクタ”).w …