detach([selector])


version 1.4 以降

解説

マッチした要素を削除します。

要素に関連付けられているキャッシュデータやイベントハンドラは削除されません。

引数

戻り値

  • Boolean: 削除した要素の jQuery オブジェクト

関連


例1:四角形をクリックすると、テキストを alert で表示します。

「detatch / append」ボタンをクリックすると、detach() で削除して追加します。要素の削除時にイベントハンドラは保持されるため、四角形をクリックすると、テキストを alert で表示します。

「remove / append」ボタンをクリックすると、remove() で削除して追加します。要素の削除時にイベントハンドラも削除されるため、四角形をクリックしても、テキストは alert 表示されなくなります。

// クリックすると、テキストを alert で表示
$(""div.detach, div.remove"").click(function(){
    alert($(this).text());
});


// detach() で削除して追加
$("#detach").click(function(){
    var parent = $("div.detach").parent();
    $("div.detach")
        .animate({opacity: 0.1})
        .detach().appendTo(parent)    // 削除(detach) -> 追加
        .delay(1000).animate({opacity: 1.0});
});

// remove() で削除して追加
$("#remove").click(function(){
    var parent = $("div.remove").parent();
    $("div.remove")
        .animate({opacity: 0.1})
        .remove().appendTo(parent)    // 削除(remove) -> 追加
        .delay(1000).animate({opacity: 1.0});
});

detach
remove