Home  >  Article  >  Web Front-end  >  Can jquery delete all child elements?

Can jquery delete all child elements?

青灯夜游
青灯夜游Original
2020-11-19 11:57:423314browse

In jquery, you can use the empty() method to delete all child elements of a specified element. The syntax format is "$(selector).empty()"; the empty() method can remove all child elements of the selected element. child nodes and content, but does not remove the element itself or its attributes.

Can jquery delete all child elements?

We know that we can use the detach() method in jQuery and the remove() method in jQuery to delete specified or matching elements, or we can use The removeClass() method and removeAttr() method delete the class or attribute of the specified or matching element. So how to delete only the sub-elements and nodes within the specified or matching element without deleting the element itself? Let me introduce it to you below.

Explanation of the empty() method in jQuery

The empty() method removes all child nodes and content of the selected element. This method does not remove the element itself, or its attributes. [Related recommendations: "jQuery Video"]

Grammar:

$(selector).empty()

Give a big "example"

Using jQuery The empty() method removes all content in the DIV

Can jquery delete all child elements?

HTML code

<p id="mochu">
  <p>这里是P标签内容</p>
  <p>这里是第二个P标签的内容</p>
</p>

JQ code

$(&#39;#button&#39;).click(function(){
  $("#mochu").empty();
})

Result image Show

The audit source code is as shown below

Can jquery delete all child elements?

Through the above picture we can find that the p tag with the ID mochu, the P tag and content inside are all been deleted!

For more programming-related knowledge, please visit: Programming Teaching! !

The above is the detailed content of Can jquery delete all child elements?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn