18910140161

多次追加template.content.clonenode(true)不起作用

顺晟科技

2021-08-12 11:36:38

234

这不起作用:

let template = document.getElementById("template"); const templateContentCloned = template.content.cloneNode(true); document.getElementById("div1").appendChild(templateContentCloned); document.getElementById("div2").appendChild(templateContentCloned); document.getElementById("div3").appendChild(templateContentCloned); <template id="template"> <p>Here is my paragraph.</p> </template> <div id="div1"></div> <div id="div2"></div> <div id="div3"></div>

但这是:

let template = document.getElementById("template"); const templateContent = template.content; document.getElementById("div1").appendChild(templateContent.cloneNode(true)); document.getElementById("div2").appendChild(templateContent.cloneNode(true)); document.getElementById("div3").appendChild(templateContent.cloneNode(true)); <template id="template"> <p>Here is my paragraph.</p> </template> <div id="div1"></div> <div id="div2"></div> <div id="div3"></div>

为什么?为什么每次要使用模板时都要克隆节点?


顺晟科技:

  • TAG:
相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航