jQuery empty node and remove node

Table of contents

1. Clear the node

1、html()

2. empty() (recommended)

2. Remove the node

3. Summary


1. Clear the node

1、html()

Not recommended, if there is an element binding event in the child node, it will cause a memory leak, which is not safe

$('#div').html('')

2. empty() (recommended)

$('#div').empty('')

2. Remove the node

$('#div').remove() // 移除#div的元素  并去除绑定的事件
  • Know the child element, to remove the parent element
$('#div').parent().remove()

3. Summary

Empty: If I empty the ul, I will empty the content in the ul, and the ul is still there

Remove: If it is removed, it is to remove the ul tag, and the ul tag does not exist

Guess you like

Origin blog.csdn.net/qq_52421092/article/details/131253242