Inserting arbitrary HTML into a DocumentFragment

前端 未结 11 2080
花落未央
花落未央 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:06

    No one ever provided the requested "easy one-liner".

    Given the variables…

    var html = '
    x
    y'; var frag = document.createDocumentFragment();

    … the following line will do the trick (in Firefox 67.0.4):

    frag.append(...new DOMParser().parseFromString(html, "text/html").body.childNodes);
    

提交回复
热议问题