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.
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
HTML code
<p id="mochu"> <p>这里是P标签内容</p> <p>这里是第二个P标签的内容</p> </p>
JQ code
$('#button').click(function(){ $("#mochu").empty(); })
Result image Show
The audit source code is as shown below
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!