[jQuery] .empty()
.empty() - 새로운 값을 가져올때 기존의 값을 제거함.
.empty()가 없을경우 기존에 값의 뒤에 새로운 값이 추가 되여, 표현이 안될수도 있음.
이게 맞는건가? ^^
.empty()Returns: jQuery
Description: Remove all child nodes of the set of matched elements from the DOM.
version added: 1.0.empty()
This method removes not only child (and other descendant) elements, but also any text within the set of matched elements. This is because, according to the DOM specification, any string of text within an element is considered a child node of that element. Consider the following HTML:
<div class="container"> <div class="hello">Hello</div> <div class="goodbye">Goodbye</div> </div>
We can target any element for removal:
$('.hello').empty();
This will result in a DOM structure with the Hello
text deleted:
<div class="container"> <div class="hello"></div> <div class="goodbye">Goodbye</div> </div>
If we had any number of nested elements inside <div class="hello">
, they would be removed, too.
To avoid memory leaks, jQuery removes other constructs such as data and event handlers from the child elements before removing the elements themselves.
If you want to remove elements without destroying their data or event handlers (so they can be re-added later), use .detach() instead.
Example:
Removes all child nodes (including text nodes) from all paragraphs
<!DOCTYPE html>
<html>
<head>
<style>
p { background:yellow; }
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<p>
Hello, <span>Person</span> <a href="javascript:;">and person</a>
</p>
<button>Call empty() on above paragraph</button>
<script>
$("button").click(function () {
$("p").empty();
});
</script>
</body>
</html>
'웹이야기' 카테고리의 다른 글
[JavaScript] 파이어폭스에서 parent.location.reload() 무한반복 현상! (0) | 2012.07.11 |
---|---|
[Javascript] 날짜 함수로 원하는 날짜 구기 (0) | 2012.07.10 |
[JavaScript] onMouse 함수들 (0) | 2012.07.09 |
주민등록번호로 성별 구별하기 (0) | 2012.07.06 |
[jQuery] 달력 만들기 - Datepicker (0) | 2012.07.05 |