목록이 있는데 여기에서 모든 자식 노드를 제거하고 싶습니다. jquery를 사용하는 가장 효율적인 방법은 무엇입니까? 이것이 내가 가진 것입니다.
<ul id='foo'>
<li>a</li>
<li>b</li>
</ul>
var thelist = document.getElementById("foo");
while (thelist.hasChildNodes()){
thelist.removeChild(thelist.lastChild);
}
한 번에 하나씩 각 항목을 제거하는 대신 바로 가기가 있습니까?
----------- 편집하다 ----------------
각 목록 요소에는 일부 데이터가 첨부되어 있으며 다음과 같은 클릭 핸들러가 있습니다.
$('#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());
}
결국 목록 항목 당 버튼을 추가 할 수도 있습니다. 그래서 empty ()가 메모리 누수가 없는지 확인하는 방법 인 것처럼 보입니다.