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

Can jquery delete all child elements?

青灯夜游
Release: 2020-11-19 13:44:40
Original
3356 people have browsed it

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()
Copy after login

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>
Copy after login

JQ code

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template