简体   繁体   English

Jquery替代DOMSubtreeModified

[英]Jquery Alternative to DOMSubtreeModified

I have the following Javascript/Jquery code: 我有以下Javascript / Jquery代码:

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

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

This does my job. 这完成了我的工作。 However, as explained here , use of DOMSubtreeModified is deprecated. 然而,正如解释在这里 ,使用DOMSubtreeModified被弃用。

To somebody new to Javascript/Jquery world, please explain ways to convert same logic into non-deprecated code. 对于刚接触Javascript / Jquery世界的人,请解释将相同逻辑转换为非弃用代码的方法。

Try this: 尝试这个:

// 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 资料来源: MDN:MutationObserver

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM