gpt4 book ai didi

javascript - 无法将appendChild与Mustache渲染模板一起使用

转载 作者:行者123 更新时间:2023-11-29 19:03:43 25 4
gpt4 key购买 nike

我有一个简单的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); 返回 html 的 String,并且您尝试将其传递给需要 appendChild节点。可能的解决方案:

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

关于javascript - 无法将appendChild与Mustache渲染模板一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44605119/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com