var udiv = document.createElement('div');
var div = document.createElement("div");
div.style.width = "100px";
div.style.height = "100px";
div.style.background = "red";
div.style.color = "white";
div.innerHTML = "Hello";
div.appendChild(udiv);
我一直在努力让这个工作,但是当我打开页面时什么也没有。我知道Javascript文件可以工作,因为其他所有内容都显示出来,但是div没有。没有错误。Javascript div无法正常工作
我已经厌倦了,它所做的只是导致这个错误:main.js:182 Uncaught TypeError:无法读取属性'appendChild'o f null –
@BubbieBell你在哪里放置你的脚本?将其置于body的结束标记下,或者使用[domcontentloaded](https://developer.mozilla.org/zh-CN/docs/Mozilla_event_reference/DOMContentLoaded_%28event%29?redirectlocale=zh-CN&redirectslug=DOM%2FDOM_event_reference%2FDOMContentLoaded )事件。 –
好吧,它现在有用!谢谢! –