首页 > 解决方案 > 使用 MutationObserver 在 DOM 元素更新上显示模式

问题描述

我只想在具有相同类的任何 DOM 元素之一被更新后才显示模式弹出窗口。我猜 MutationObserver 是最好的工具吗?这是我到目前为止所得到的,但它不起作用......

// Get the modal
var modal = document.getElementById("save-search-modal");
    
// select the target nodes for mutation observing
var target = document.getElementsByClassName("sidx-pill-value")[0];
    
// When .sidx-pill-value is updated, open the modal
var observer = new MutationObserver(function(mutations) {
    mutations.forEach(function(mutation) {
        modal.style.display = "block";
    });
});

// configuration of the observer:
var config = { subtree: true, characterData: true, attributes: true, childList: true }

// pass in the target node, as well as the observer options
observer.observe(target, config);

标签: javascriptmutation-observers

解决方案


推荐阅读