admin管理员组文章数量:1402129
I have the following Javascript/Jquery code:
<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.
To somebody new to Javascript/Jquery world, please explain ways to convert same logic into non-deprecated code.
I have the following Javascript/Jquery code:
<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.
To somebody new to Javascript/Jquery world, please explain ways to convert same logic into non-deprecated code.
Share Improve this question asked Apr 30, 2018 at 19:31 inquilabeeinquilabee 7913 gold badges12 silver badges23 bronze badges 2- 2 try to follow the "Example usage" on MutationObserver, – DIEGO CARRASCAL Commented Apr 30, 2018 at 19:48
- 1 Look here... Probably relevant to you: stackoverflow./a/14570614/2159528 – Louys Patrice Bessette Commented Apr 30, 2018 at 20:16
1 Answer
Reset to default 4Try 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
本文标签: javascriptJquery Alternative to DOMSubtreeModifiedStack Overflow
版权声明:本文标题:javascript - Jquery Alternative to DOMSubtreeModified - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744345977a2601760.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论