Inserting arbitrary HTML into a DocumentFragment

前端 未结 11 2112
花落未央
花落未央 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 = '
    x
    y
    '; var frag = document.createDocumentFragment(); frag.appendChild($​(html)[0]);

提交回复
热议问题