例如,如果我有一个 html 文件,它就是
<html>
</html>
我有类似的东西
const div=document.createElement('div');
如何将 div 附加到空 html 文件? 我尝试过执行 document.appendChild(div),但这不起作用。
最佳答案
通过选择器查询获取Html元素并附加div
const div = document.createElement('div');
const htmlElement = document.querySelector("html");
htmlElement.appendChild(div)
或者只是:
const div = document.createElement('div');
document.documentElement.appendChild(div);
document.documentElement 获取html元素
关于javascript - 如何使用 JavaScript 将元素附加到空 html 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61038003/