初心者向けjQuery入門

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

未分類

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

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

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

jQuery wrap 親要素を追加

wrapは親要素を追加します

サンプル

$(“セレクタ”).wrap(‘タグ’)

サンプル

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

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

$(“.red”).wrap(‘<div></div>’)
$(“div”).css(“color”,”red”)

 

});
</script>
<title>

</title>
</head>
<body>

<h3 class=”red”>red</h3>
<h3>Green</h3>
<h3>yellow</h3>
<h3>Blue</h3>

 

</body>
</html>

実行結果は

divが追加されてdivにcssで赤に設定されています。

 

jQuery unwrap親要素を削除

unwrapは親要素を削除します。

サンプル

$(“セレクタ”).unwrap(‘タグ’)

サンプル

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

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

$(“.red”).wrap(‘<div></div>’)
$(“.red”).unwrap(‘<div></div>’)
$(“div”).css(“color”,”red”)

 

});
</script>
<title>

</title>
</head>
<body>

<h3 class=”red”>red</h3>
<h3>Green</h3>
<h3>yellow</h3>
<h3>Blue</h3>

 

</body>
</html>

実行結果は

色は変わりません。

divを作成して削除されているのでdivにかけているcssは適応されず色は変わりません。

 

 

スポンサードリンク




スポンサードリンク




-未分類
-

執筆者:


comment

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

関連記事

no image

jQuery attrとremoveAttr属性の取得と設定と削除

jQuery attrとremoveAttr属性の取得と設定と削除 attrはattributeの略ですね。 英語で属性という意味です。 jQuery attr属性の設定する サンプル $(&#822 …

no image

jQuery セレクタでクラスとIDと要素を指定する方法

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

no image

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

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

no image

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

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

no image

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

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