Inserting arbitrary HTML into a DocumentFragment

前端 未结 11 1931
花落未央
花落未央 2020-11-29 18:53

I know that adding innerHTML to document fragments has been recently discussed, and will hopefully see inclusion in the DOM Standard. But, what is the workaround you\'re sup

相关标签:
11条回答
  • 2020-11-29 19:13

    To do this with as little lines as possible, you could wrap your content above in another div so you do not have to loop or call appendchild more than once. Using jQuery (as you mentioned is allowed) you can very quickly create an unattached dom node and place it in the fragment.

    var html = '<div id="main"><div>x</div><span>y</span></div>';
    var frag = document.createDocumentFragment();
    frag.appendChild($​(html)[0]);
    
    0 讨论(0)
  • 2020-11-29 19:14

    Here is a x-browser solution, tested on IE10, IE11, Edge, Chrome and FF.

        function HTML2DocumentFragment(markup: string) {
            if (markup.toLowerCase().trim().indexOf('<!doctype') === 0) {
                let doc = document.implementation.createHTMLDocument("");
                doc.documentElement.innerHTML = markup;
                return doc;
            } else if ('content' in document.createElement('template')) {
                // Template tag exists!
                let el = document.createElement('template');
                el.innerHTML = markup;
                return el.content;
            } else {
                // Template tag doesn't exist!
                var docfrag = document.createDocumentFragment();
                let el = document.createElement('body');
                el.innerHTML = markup;
                for (let i = 0; 0 < el.childNodes.length;) {
                    docfrag.appendChild(el.childNodes[i]);
                }
                return docfrag;
            }
        }
    
    0 讨论(0)
  • 2020-11-29 19:17
    var html = '<div>x</div><span>y</span>';
    var frag = document.createDocumentFragment();
    var e = document.createElement('i');
    frag.appendChild(e);
    e.insertAdjacentHTML('afterend', html);
    frag.removeChild(e);
    
    0 讨论(0)
  • 2020-11-29 19:18

    @PAEz pointed out that @RobW's approach does not include text between elements. That's because children only grabs Elements, and not Nodes. A more robust approach might be as follows:

    var fragment = document.createDocumentFragment(),
        intermediateContainer = document.createElement('div');
    
    intermediateContainer.innerHTML = "Wubba<div>Lubba</div>Dub<span>Dub</span>";
    
    while (intermediateContainer.childNodes.length > 0) {
        fragment.appendChild(intermediateContainer.childNodes[0]);
    }
    

    Performance may suffer on larger chunks of HTML, however, it is compatible with many older browsers, and concise.

    0 讨论(0)
  • 2020-11-29 19:24

    createDocumentFragment creates an empty DOM "container". innerHtml and other methods work only on DOM nodes (not the container) so you have to create your nodes first and then add them to the fragment. You can do it using a painful method of appendChild or you can create one node and modify it's innerHtml and add it to your fragment.

    var frag = document.createDocumentFragment();
        var html = '<div>x</div><span>y</span>';
    var holder = document.createElement("div")
    holder.innerHTML = html
    frag.appendChild(holder)
    

    with jquery you simply keep and build your html as a string. If you want to convert it to a jquery object to perform jquery like operations on it simply do $(html) which creates a jquery object in memory. Once you are ready to append it you simply append it to an existing element on a page

    0 讨论(0)
提交回复
热议问题