JQuery替代DOMSubtreeModified的方法

3

我有以下的Javascript/Jquery代码:

       <script type="text/javascript">
            function ChangeMathOnPage() {
                MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
            }

            $('.markdownx-preview').each(function(){
                $(this).on('DOMSubtreeModified', ChangeMathOnPage);
            });
        </script>

这个可以完成我的工作。但是,正如这里所解释的那样,DOMSubtreeModified的使用已经被弃用。
对于新手来说,可以将相同的逻辑转换为非弃用代码,请解释一下。

2
尝试按照MutationObserver上的“使用示例”进行操作。 - DIEGO CARRASCAL
1
看这里... 可能与您相关: https://dev59.com/q3A75IYBdhLWcg3wlqAT#14570614 - Louys Patrice Bessette
1个回答

3

试试这个:

// Select the node that will be observed for mutations
var targetNode = document.getElementById('some-id');

// Options for the observer (which mutations to observe)
var config = { attributes: true, childList: true };

// Callback function to execute when mutations are observed
var callback = function(mutationsList) {
    for(var mutation of mutationsList) {
        if (mutation.type == 'childList') {
            console.log('A child node has been added or removed.');
        }
        else if (mutation.type == 'attributes') {
            console.log('The ' + mutation.attributeName + ' attribute was modified.');
        }
    }
};

// Create an observer instance linked to the callback function
var observer = new MutationObserver(callback);

// Start observing the target node for configured mutations
observer.observe(targetNode, config);

// Later, you can stop observing
observer.disconnect();

Source: MDN: MutationObserver


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接