How jquery removes elements
This article mainly explains "how to remove elements from jquery". The content in the article is simple and clear, easy to learn and understand. Please follow the editor's train of thought to study and learn "how to remove elements from jquery".
Method: 1, use the remove () method to remove the element, this method is used to delete the specified element, the syntax is "element object .remove ();"; 2, use the empty () method to remove the element, this method is used to delete all child elements of the specified element, the syntax is "element object .empty ();".
The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.
How does jquery remove elements?
You can use the remove () and empty () methods of jquery
Remove () deletes selected elements and child elements
Empty () deletes only the children of the selected element
Let's take a look at each of them:
1. Using remove method
The remove () method removes the selected element, including all text and child nodes.
This method also removes the data and events of the selected element.
The syntax is:
(selector) .remove ()
Examples are as follows:
$(document) .ready (function () {$("button") .click (function () {$("p"). Remove ();})
This is a paragraph.
This is another paragraph.
Remove all P elements
Output result:
2. Using empty method
The empty () method removes all child nodes and content of the selected element.
Note: this method does not remove the element itself or its attributes.
The syntax is:
(selector) .empty ()
Examples are as follows:
(document) .ready (function () {$("button") .click (function () {$("div"). Empty ();}); this is some text.
This is a paragraph in the div block.
This is a paragraph outside the div block.
Remove the contents of the div block
Output result:
Thank you for your reading, the above is the content of "how to remove elements from jquery". After the study of this article, I believe you have a deeper understanding of how to remove elements from jquery, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!