Home > Article > Web Front-end > How to use detach removal in jquery
In jquery, the detach method can be used to remove a selected element and remove all text and child nodes of the element, but the data and events will be retained and allowed to be reinserted later. The syntax is "$(selector) .detach()"; if you only need to remove content from the selected element, use the empty() method.
The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.
detach() method to remove selected elements, including all text and child nodes. It then persists the data and events.
This method keeps a copy of the removed elements, allowing them to be reinserted later.
Tip: If you need to remove an element and its data and events, use the remove() method instead.
Tip: If you only want to remove content from the selected elements, use the empty() method.
Grammar
$(selector).detach()
The example is as follows:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>123</title> <script src="js/jquery.min.js"> </script> <script> $(document).ready(function(){ $("button").click(function(){ $("p").detach(); }); }); </script> </head> <body> <p>这是一个段落。</p> <p>这是另一个段落。</p> <button>移除所有P元素</button> </body> </html>
Output result:
Related video tutorial recommendations: jQuery video tutorial
The above is the detailed content of How to use detach removal in jquery. For more information, please follow other related articles on the PHP Chinese website!