使用jQuery的empty()方法,我可以阻止从DOM中删除特定元素,同时删除所有其他元素吗?
例如:
<div id="container">
<div id="noRemove"></div>
... more content ...
</div>
Run Code Online (Sandbox Code Playgroud)
当我使用jQuery进行此调用时$("#container").empty(),如何在删除noRemove内部的其余内容时阻止删除container?
ban*_*aka 11
您不能empty单独使用该功能.这是你可以做到的一种方式:
var $container = $('#container'),
$noRemove = $container.find('#noRemove');
$container.html($noRemove);
Run Code Online (Sandbox Code Playgroud)
这是一个小提琴:http://jsfiddle.net/joplomacedo/R4cu5/
使用以下内容,它将删除容器中的所有内容,但其元素id为noRemove:
$('#container').contents().filter(function () {
return this.id != "noRemove";
}).remove();
Run Code Online (Sandbox Code Playgroud)
演示.
| 归档时间: |
|
| 查看次数: |
3920 次 |
| 最近记录: |