初心者向けjQuery入門

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

未分類

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

投稿日:

jQuery first-childとlast-child

jQuery first-child

first-childは一番最初の子要素を指定します。
$(“要素:first-child “)
要素を:でつなきます。
サンプル

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

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

$(“h3:first-child “).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>

最初のredが72pxとイタリックになってますね。

よくliでサンプルかかれてるけどh3の最初とかこんな使い方もできます。

jQuery last-child

last-childは一番最後のの子要素を指定します。

$(“要素:last-child “)
要素を:でつなきます。

サンプル

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

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

$(“h3:last-child “).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>

最後のBlueが72pxとイタリックになってますね。

スポンサードリンク




スポンサードリンク




-未分類
-,

執筆者:


comment

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

関連記事

no image

jQuery eq要素のインデックスを取得する

jQuery eq要素のインデックスを取得する jQuery eq要素のインデックスを取得する eqは指定した要素のインデックスを指定する。 サンプル $(“セレクタ”).eq …

no image

jQuery hoverマウスと乗せたり外した時動作するイベント

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

no image

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

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

no image

jQuery removeClassクラスを削除する方法

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

no image

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

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