简体   繁体   中英

Changing element's HTML before it's inserted in the DOM

Is it possible to change an element's inner HTML before it is inserted in the DOM? I already tried doing this with MutationObserver but the problem is that you can see the element's HTML visually changing, is there a way to do this before DOM insertion altogether?

var observer = new MutationObserver(function(mutations) {

    mutations.forEach(function(mutation) {
        mutation.addedNodes[0].innerHTML = "....";
    });    
});

// Notify me of everything!
var observerConfig = {
    attributes: true, 
    childList: true, 
    characterData: true 
};

var targetNode = document.querySelector("ul#myElement");
observer.observe(targetNode, observerConfig);

您可以尝试将整个正文显示为CSS display:none,更改所需的内容,然后再将其放回display:block

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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