Javascript WebComponents:删除克隆的模板

Javascript WebComponents:删除克隆的模板,javascript,html,clone,web-component,Javascript,Html,Clone,Web Component,我正在使用webcomponentsjs polyfill。无x标签、聚合物等,最好是香草JS 克隆模板并将其附加到文档后,我无法再次删除它,因为它缺少parentNode var tmpl = document.getElementById('tmpl'); var clone = document.importNode(tmpl.content, true); document.body.appendChild(clone); console.log(clone.parentNode); /

我正在使用webcomponentsjs polyfill。无x标签、聚合物等,最好是香草JS

克隆模板并将其附加到文档后,我无法再次删除它,因为它缺少parentNode

var tmpl = document.getElementById('tmpl');
var clone = document.importNode(tmpl.content, true);
document.body.appendChild(clone);
console.log(clone.parentNode); // parentNode is null (not undefined!)
clone.parentNode.removeChild(clone); // fails!
我的问题是:如何再次删除该元素。我遗漏了什么吗?

你把vs.搞混了。模板的内容显然是DocumentFragment的一个实例:

添加涉及ShadowDOM的模板内容有一种常见的方法:

var shadow = document.querySelector('#container').createShadowRoot();
shadow.appendChild(document.querySelector('#tmpl').content, true);
或不使用ShadowDOM,按原样插入:

document.querySelector('#container').appendChild(
  document.querySelector('#tmpl').content, true
);
希望能有帮助

var shadow = document.querySelector('#container').createShadowRoot();
shadow.appendChild(document.querySelector('#tmpl').content, true);
document.querySelector('#container').appendChild(
  document.querySelector('#tmpl').content, true
);