我有一个列表,我只想从其中删除所有子 node .使用jQuery最有效的方式是什么?这就是我所拥有的:
<ul id='foo'>
<li>a</li>
<li>b</li>
</ul>
var thelist = document.getElementById("foo");
while (thelist.hasChildNodes()){
thelist.removeChild(thelist.lastChild);
}
is there a shortcut rather than removing each item, one at a time?
----------- Edit ----------------
每个列表元素都附加了一些数据,还有一个如下的点击处理程序:
$('#foo').delegate('li', 'click', function() {
alert('hi!');
});
// adds element to the list at runtime
function addListElement() {
var element = $('<li>hi</hi>');
element.data('grade', new Grade());
}
最后,我可能还会 for each 列表项添加按钮——所以看起来空()是确保没有内存泄漏的方法吗?