初心者向けjQuery入門

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

未分類

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

投稿日:2017年8月9日 更新日:

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

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

addclassクラスは要素にクラスを設定できます。
どういうことかというこクラスのタブにhtmlで書かなくても動的にクラスが追加されます。

$(“セレクタ”).addClass(“クラス名”)

サンプル

<html>
<head>

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

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

$(“h3:last-child “).addClass(“last”)
$(“.last”).css({“font-size”:”72px”,”font-style”:”italic”,”background-color”:”red”})

});
</script>

<title>

</title>
</head>
<body>
<div>
<h3>red</h3>
<h3>Green</h3>
<h3>yellow</h3>
<h3>Blue</h3>
</div>

</body>

</html>

h3:last-childでh3の最後の要素にlastをいうクラス名が設定されました。 クラスlastにはイタリックにして背景が赤とフォントが72pxに設定していますので表示が変わってます。

色を変えなくてもクロームの検証を押せばクラスが追加されているか確認することができます。

スポンサードリンク




スポンサードリンク




-未分類
-

執筆者:


comment

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

関連記事

no image

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

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

no image

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

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

no image

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

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

no image

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

jQuery first-childとlast-child jQuery first-child first-childは一番最初の子要素を指定します。 $(“要素:first-child …

no image

jQuery wrapとunwrap親要素を追加と削除

jQuery wrapとunwrap親要素を追加と削除 jQuery wrap 親要素を追加 wrapは親要素を追加します サンプル $(“セレクタ”).wrap(&#8216 …