初心者向け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 hoverマウスと乗せたり外した時動作するイベント

jQuery hoverマウスと乗せたり外した時動作するイベント jQuery hoverマウスと乗せたり外した時動作するイベント hoverはマウスを乗せたり外したりしたときに起動するイベントです。 …

no image

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

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

no image

jQuery animate要素をアニメーションさせる

jQuery animate要素をアニメーションさせる jQuery animate要素をアニメーションさせる animateの英語の意味は 生命を吹き込む,活気づける、活発にする、励ます、動画にする …

no image

jQuery mouseoverとmouseoutマウスイベントの方法

Query mouseoverとmouseoutマウスイベントの方法 Query mouseoverマウスを乗せた時のイベントの方法 mouseoverはマウスを要素に乗せた時に発生するイベントです。 …

no image

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

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