Home  >  Article  >  Web Front-end  >  The difference between empty and remove in DOM node deletion

The difference between empty and remove in DOM node deletion

一个新手
一个新手Original
2017-09-29 10:04:011831browse

Removing nodes on the page is a common operation for developers. jQuery provides several different methods to deal with this problem. Here we take a closer look at the empty and remove methods

empty As the name suggests, empty method, but it is a little different from delete, because it only removes all child nodes in the specified element.

This method not only removes child elements (and other descendant elements), but also removes the text within the element. Because, according to the description, any text string in an element is regarded as a child node of the element. Please look at the following HTML:


这是p标签

If we remove all elements of p inside via empty method, it just clears the inner html code, but the markup still remains in the DOM


//通过empty处理
$('.hello').empty()
//结果:

这是p标签

被移除

通过empty移除了当前p元素下的所有p元素,但是本身id=test的p元素没有被删除



    
    
    
    

通过empty移除元素

p元素1

p元素2

remove is the same as empty, both are ways to remove elements, but remove will remove the element itself, and also Everything inside the element will be removed, including bound events and jQuery data related to the element.

For example, for a node, bind a click event


这是P段落

$('.hello').on("click",fn)

It is actually very simple to delete this node without using the remove method, but at the same time, the event needs to be destroyed. Removed, this is to prevent "memory leaks", so front-end developers must pay attention to how many events are tied, and remember to destroy it when not in use

Remove p and all its internal elements through the remove method, remove The event destruction method will be automatically operated internally, so it is very simple to use


//通过remove处理
$('.hello').remove()
//结果:

这是P段落

全部被移除 //节点不存在了,同事事件也会被销毁

remove expression parameters:

remove is easier to use than empty. Pass a selector expression to filter the set of matching elements that will be removed. You can selectively delete the specified node

We can select a group of the same elements through $(), and then use remove() Pass the filtering rules to process



    
    
    
    

通过jQuery remove方法移除元素

p元素1

p元素2

p元素3

p元素4

When you want to remove the specified element, jQuery provides two methods: empty() and remove([expr]) , both delete elements, but there are still differences between the two:

When it is necessary to remove the specified element, jQuery provides two methods, empty() and remove([expr]), two Both delete elements, but there are still differences between the two

empty method

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

  • 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

The above is the difference between the two. Let’s deepen our understanding through the code part on the right.



    
    
        
    

通过empty与remove移除元素

p元素1

p元素2

p元素3

p元素4

The above is the detailed content of The difference between empty and remove in DOM node deletion. 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