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

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

no image

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

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

no image

jQuery fadeInとfadeOutフェードインとフェードアウトする方法

jQuery fadeInとfadeOutフェードインとフェードアウトする方法 jQuery fadeInフェードインする方法 フェードインの英語の意味は次第に明るくなる、次第にはっきりするです。 f …

no image

jQuery firstとlast最初と最後の要素を取得する

jQuery firstとlast最初と最後の要素を取得する jQuery first最初の要素を取得する firstメソッドは最初の要素を取得します。 サンプル $(“セレクタ&#822 …