jQuery中的empty()个方法和remove()个方法有什么区别?当我们调用其中任何一个方法时,所创建的对象将被销毁,内存将被释放?

推荐答案

  • empty() will empty the selection of its contents, but preserve the selection itself.
  • remove() will empty the selection of its contents and remove the selection itself.

考虑:

<div>
    <p><strong>foo</strong></p>
</div>

$('p').empty();  // --> "<div><p></p></div>"

// whereas,
$('p').remove(); // --> "<div></div>"

Both of them remove the DOM objects and should release the memory they take up, yes.


Here are links to documentation, which also contains examples:

Jquery相关问答推荐

try 使用jQuery AJAX将参数传递给http Post方法时出现未知错误

JQuery日期 Select 器未设置从jQuery中 Select 的月份起90天或3个月

JQuery DateTimePicker-在 Select 中抓取星期几

bootstrap - Select Select 器不能在下拉菜单中 Select

从克隆的输入中读取数据属性,返回初始输入的值

添加动态表单后,Select2 消失

jQuery在localStorage时将类添加到正文

jQuery:获取数据属性

扩展 jQuery 插件的最佳方式

如何设置缓存:jQuery.get 调用中的 false

jQuery JSON到字符串?

如何在不导致页面滚动的情况下删除位置哈希?

如何在 JQuery UI 自动完成中使用 source:function()... 和 AJAX

即使通过Javascript代码判断,如何触发复选框单击事件?

jquery-ui datepicker更改z-index

如何清除/重置 jQuery UI Datepicker 日历上的选定日期?

使用 JavaScript 获取用户代理

使用 jQuery 将事件侦听器添加到动态添加的元素

如何在没有鼠标事件的情况下在 jQuery 中获取鼠标位置?

使用 TypeScript 设置 window.location