-1

我正在使用 javascript 模板构建一小段 html 文档。const button下面的代码是我的模板。如您所见,我使用替换替换了一些内容。const button成为<button>label</button>,这很好。

const button = `<button>{try}</button>`;
const newButton = button.replace('{try}', 'label');
const body = document.querySelector('body');

现在我想在 body 标签内附加我的 bew 按钮。

body.append(newButton);

它可以工作,但是当我刷新浏览器时,我看到“ <button>label</button>”,但我想要这个 html 的渲染版本,现在我看不到一个按钮,只有一个字符串<button>...。如何渲染此按钮?

4

1 回答 1

1

怎么用insertAdjacentHTML

const button = `<button>{try}</button>`;
document.body.insertAdjacentHTML(`beforeend`, button.replace('{try}', 'label'));

于 2021-05-25T09:22:59.433 回答