我正在尝试使用模板元素在 A-Frame 中动态创建新实体。辅助函数如下所示:
function htmlToElement(html) {
const template = document.createElement('template');
// Never return a text node of whitespace as the result.
html = html.trim();
template.innerHTML = html;
return template.content.firstChild;
}
我像这样使用它:
document.querySelector('a-scene').addEventListener('loaded', () => {
const cube = htmlToElement('<a-box position="0 1 -2"></a-box>')
document.querySelector('a-scene').appendChild(cube);
});
这在 Firefox 中效果很好,但在 Chrome 中实体不显示。如果查看具有类似document.querySelector('a-box')
没有object3D
属性的实体,那么我不认为将元素附加到场景中是在 Chrome 上使用 A-Frame 注册元素。我错过了什么才能让它在 Chrome 中工作?这是一支笔。
谢谢!
也许您可以尝试使用 insertAdjacentHTML?那时您可能不需要模板包装器。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句