jQuery detach()Methods

jQuery HTML/CSS 方法jQuery HTML/CSS Methods

Examples

Remove all <p> elements:

$("button").click(function(){ $("p").detach(); });

Try it Yourself »

Definition and Usage

The detach() method removes the selected elements, including all text and child nodes. It then preserves the data and events.

This method keeps a copy of the removed elements, allowing them to be reinserted later.

Tip:To remove elements and their data and events, use theremove()method instead.

Tip:If you only need to remove content from the selected elements, use theempty()method.


Syntax

$(selector).detach()


实例

More Examples

Remove and restore an element
How to use the detach() method to remove and restore an element.

The difference between detach() and remove()
Demonstrate the difference between the detach() and remove() methods.


jQuery HTML/CSS 方法jQuery HTML/CSS Methods

Other extensions