LoginSignup
30
23

More than 5 years have passed since last update.

【jQuery】親要素を削除する

Posted at

親要素を削除したいときに、どうすればいいのかわかったのでメモ。
下記のようなHTMLのときに、pをクリックしたらdivを消すとする。

<div>
<p>ここをクリックする</p>
</div>

remove()は×

$('div p').click(function(){
    $(this).parent().remove(); //divとその中身が全て消えちゃう
});

unwrap()を使う

$('div p').click(function(){
    $(this).unwrap(); //divだけを消す
});

※jQuery1.4系以降対応

デモ:http://jsdo.it/Ituki/dr43
参考:http://h2ham.seesaa.net/article/143970370.html

30
23
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
30
23