最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - HTML Template element vs document.createDocumentFragment - Stack Overflow

programmeradmin1浏览0评论

I'm trying to figure out the difference between using document.createDocumentFragment versus using an HTML <tamplate> element.

Is there a difference between them in behavior or performance?

I'm trying to figure out the difference between using document.createDocumentFragment versus using an HTML <tamplate> element.

Is there a difference between them in behavior or performance?

Share Improve this question asked Dec 2, 2018 at 6:14 MendyMendy 8,6926 gold badges31 silver badges46 bronze badges 1
  • 1 Found a useful link. coderwall./p/o9ws2g/… – Sameer Commented Dec 2, 2018 at 6:40
Add a ment  | 

1 Answer 1

Reset to default 11

Both <template> and document.createDocumentFragment are used for storing HTML-like data without rendering it, but the use cases are somewhat different.

The <template> tag's main purpose is to, as the name applies, store HTML for a later time, and or to be used repeatedly across the document. This tag is useful when using a template engine where the contents are usually never changed but the input may be different.

document.createDocumentFragment is used to create an entire DOM tree in JS without the browser rendering it, while still having the ability to use the DOM API with it. This useful when dynamically generating HTML by leveraging the DOM API, and to later inject the results in the actual document's DOM.

More: Template Tag and DocumentFragment

发布评论

评论列表(0)

  1. 暂无评论