--- title: MutationObserver slug: Web/API/MutationObserver translation_of: Web/API/MutationObserver ---
{{APIRef("DOM")}}
MutationObserver 는 개발자들에게 DOM 변경 감시를 제공합니다. DOM3 이벤트 기술 설명서에 정의된 Mutation Events 를 대체합니다.
MutationObserver()새 DOM 변경 감시자의 인스턴스화에 대한 생성자.
new MutationObserver( function callback );
callbackMutationObserver 인스턴스 입니다.void observe( {{domxref("Node")}} target, MutationObserverInit options ); |
void disconnect(); |
Array takeRecords(); |
주: {{domxref("Node")}}의 대상은 NodeJS 와 혼동되지 말아야 합니다.
observe()특정 노드에서 DOM 변경의 알림을 받을 수 있도록 MutationObserver 인스턴스를 등록.
void observe(
{{domxref("Node")}} target,
MutationObserverInit options
);
targetoptionsMutationObserverInit 객체.disconnect()DOM 변경 알림을 받는 MutationObserver 인스턴스 중지. observe() 재사용시까지 감시 콜백은 발동되지 않을 것입니다.
void disconnect();
takeRecords()MutationObserver 인스턴스의 레코드 큐를 비우고 안에 든것을 반환합니다.
Array takeRecords();
{{domxref("MutationRecord")}}들의 배열을 반환.
MutationObserverInitMutationObserverInit 는 다음 속성들을 가진 객체입니다:
childList, attributes, 또는 characterData는 true로 설정해야합니다. 그렇지 않으면 'An invalid or ilegal string was specified' 오류가 발생합니다. | 속성 | 설명 |
childList |
Set to true if additions and removals of the target node's child elements (including text nodes) are to be observed. |
attributes |
Set to true if mutations to target's attributes are to be observed. |
characterData |
Set to true if mutations to target's data are to be observed. |
subtree |
Set to true if mutations to target and target's descendants are to be observed. |
attributeOldValue |
Set to true if attributes is set to true and target's attribute value before the mutation needs to be recorded. |
characterDataOldValue |
Set to true if characterData is set to true and target's data before the mutation needs to be recorded. |
attributeFilter |
Set to an array of attribute local names (without namespace) if not all attribute mutations need to be observed. |
다음 예제는 이 블로그 글에서 발췌했습니다.
// 대상 node 선택
var target = document.getElementById('some-id');
// 감시자 인스턴스 만들기
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
console.log(mutation.type);
});
});
// 감시자의 설정:
var config = { attributes: true, childList: true, characterData: true };
// 감시자 옵션 포함, 대상 노드에 전달
observer.observe(target, config);
// 나중에, 감시를 중지 가능
observer.disconnect();
| 규격 | 상태 | 의견 |
|---|---|---|
| {{SpecName('DOM WHATWG', '#mutationobserver', 'MutationObserver')}} | {{ Spec2('DOM WHATWG') }} | |
| {{SpecName('DOM4', '#mutationobserver', 'MutationObserver')}} | {{ Spec2('DOM4') }} |
{{Compat("api.MutationObserver")}}