初心者向け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 セレクタでクラスとIDと要素を指定する方法

jQuery セレクタでクラスとIDと要素を指定する方法 jQuery セレクタ jQueryでは要素にアクセスするにはセレクタを指定しなければいけません。 要素を指定するには$()でセレクタを指定し …

no image

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

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

no image

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

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

no image

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

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

no image

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

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