Trung BìnhJavaScript iconJavaScript

MutationObserver là gì?

MutationObserver theo dõi thay đổi trên DOM tree (attribute changes, child additions/removals, text changes).

Asynchronous, callbacks chạy như microtask.

javascript
const observer = new MutationObserver(mutations => {
  mutations.forEach(m => console.log(m.type, m.target));
});
observer.observe(document.body, { childList: true, subtree: true });
// observer.disconnect() để dừng

Hữu ích khi làm việc với third-party code thay đổi DOM, implement undo/redo, hay real-time updates.

ResizeObserver theo dõi thay đổi kích thước element.

Xem toàn bộ JavaScript cùng filter theo level & chủ đề con.

Mở danh sách JavaScript