初心者向けjQuery入門

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

未分類

jQuery removeとemptyの違い

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

removeとemptyの違い

removeとemptyの違い

removeとemptyの違い。
removeは要素をDOMから削除します。
emptyは要素を子ノードから削除します。

remove 要素をDOMから削除する

removeの英語の意味は取り除くです。

DOMから削除するといわれてもピンとこないかもしれません。

簡単に言うとタグが削除された状態になります。

サンプル

$(“セレクタ”).remove()

サンプル

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

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

$(“.Green”).remove()

 

});
</script>
<title>

</title>
</head>
<body>

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

 

</body>
</html>

 

実行結果は

red

yellow

Blue

Greenクラスが削除されてますね。

見た目はわかりませんがタグの構造的に全てきえているということです。

jQueryオブジェクトからは消えていないのでマッチはします。

 

empty要素を子ノードから削除する

emptyの英語の意味は空です。

サンプル

$(“セレクタ”).empty()

サンプル

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

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

$(“.Green”).empty()

 

});
</script>
<title>

</title>
</head>
<body>

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

 

</body>
</html>

実行結果は

red

yellow

Blue

です。

子ノードを消すのでdomのテキストノードとか

全て空になっていると思われます。

 

 

 

 

スポンサードリンク




スポンサードリンク




-未分類
-

執筆者:


comment

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

関連記事

no image

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

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

no image

jQuery textメソッドとhtmlの違い 文字列を変更する

jQuery textメソッド 文字列を変更する jQuery textメソッド 文字列を変更する textメソッドは要素のtextを変更することができます。 $(“セレクタ” …

no image

jQuery mouseoverとmouseoutマウスイベントの方法

Query mouseoverとmouseoutマウスイベントの方法 Query mouseoverマウスを乗せた時のイベントの方法 mouseoverはマウスを要素に乗せた時に発生するイベントです。 …

no image

jQuery parentとchildren親と子要素を取得する

Query parentとchildren親と子要素を取得する Query parent 親要素を取得する parentは指定した要素の親要素を取得します、 サンプル $(“セレクタ&#8 …

no image

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

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