English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

jQuery detach() 方法

jQuery HTML/CSS Methods

The detach() method removes the selected element, including all text and child nodes. Then it retains the data and events.

The detach() method removes the selected element, including all text and child nodes. Then it retains the data and events.

However, detach() retains all jQuery data and events associated with the removed element.

This method retains a copy of the removed element, allowing it to be reinserted later.To remove the element and its data and events, useMethod.

To only remove content from the selected element, useempty()Method.

Syntax:

$(selector).detach()

Instance

Remove all paragraphs from the DOM:

$("button").click(function(){
  $("p").detach();
});
Test See‹/›

Use the detach() method to delete and restore elements:

$("#btn1").click(function(){
  elem = $("p").detach();
});
$("#btn2").click(function(){
  $("body").prepend(elem);
});
Test See‹/›

jQuery HTML/CSS Methods