不能将 appendChild 与 Mustache 呈现的模板一起使用

Can't use appendChild with Mustache rendered template

我有一个简单的 Mustache.js 模板:

var template = '<div style="width: 300px; height: 200px; background: red;">TEST</div>';

var rendered = Mustache.render(template);

我想用 appendChild 将其添加到正文中:

document.body.appendChild(rendered);

但这会产生错误:

Uncaught TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'

Mustache.render(template); returns html 的 String 并且您尝试将其传递给需要 NodeappendChild。可能的解决方案:

var template = '<div style="width: 300px; height: 200px; background: red;">TEST</div>';
document.body.insertAdjacentHTML('beforeend', Mustache.render(template));