The difference between jQuery DOM operation empty and remove

When you want to remove specified elements, jQuery provides two methods, empty() and remove([expr]). Both of them delete elements, but there are still differences between them.

empty Method

Strictly speaking, the empty() method does not delete the node, but clears the node. It can clear all descendant nodes in the element

Strictly speaking, empty cannot delete its own node

remove method

This node and all descendant nodes contained in this node will be deleted at the same time

Provides a filtering expression to be passed to delete elements in the specified collection

Below we will use code to analyze in detail

    remove()与empty()的区别   

元素1

元素2

元素3

元素4

The above code, after we run it, we can see that when I click empty, element 1 and element 2 will be cleared, but the div still exists ’

When I click remove, not only the elements inside will be deleted, but also the div itself will be deleted

Continuing Learning
||
remove()与empty()的区别

元素1

元素2

元素3

元素4

submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!