初心者向け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 beforeとafterの違いと要素の追加

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

no image

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

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

no image

jQuery ダウンロードとCDNファイルを読み込みむ方法とready

jQuery ダウンロードと ファイルを読み込みむ方法 jQuery ダウンロード jQueryをダウンロードするには公式サイトはhttps://jquery.com/こちらです。 ダウンロードするフ …

no image

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

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

no image

jQuery widthとheight要素の横と縦を設定する

jQuery widthとheight要素の横と縦を設定する jQuery width 要素のサイズの横幅を設定するにはwidthメソッドを使用します。 $(“セレクタ”).w …