初心者向け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 removeClassクラスを削除する方法

jQuery removeClassクラスを削除する方法 jQuery removeClassクラスを削除する方法 htmlで設定したクラスを削除します。 cssで設定したクラスでも削除はできます。 …

no image

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

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

no image

jQuery prevとnext前と後の要素取得する

jQuery prevとnext前と後の要素取得する jQuery prev前の要素取得する prevは英語の略語で previous(以前の) 等です。 prevは指定した要素の前の要素を取得します …

no image

jQuery 隣接セレクタと子セレクタのチャイルドセレクタ

jQuery 隣接セレクタと子セレクタのチャイルドセレクタ jQuery 隣接セレクタ 隣接セレクタとは隣に接しているセレクタの二つ目の要素を指定します。 隣り合っているとなっていますが前後にあるセレ …

no image

jQuery prependとappendの違いと要素の追加

jQuery prependとappendの違いと要素の追加 jQuery prependとappendの違い prependとappendの違いはprependは指定した要素の要素の先頭に追加されま …