简体   繁体   中英

Javascript div doesn't work

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);

I've been trying to get this to work, but when I open the page there's nothing there. I know the Javascript file works because everything else shows up, but the div doesn't. There's no error.

您已将udiv附加到div ,但从未将div附加到 DOM。

But both are new elements, Append those elements to an already existing element or to the body ,

.
.
.
div.appendChild(udiv);
document.body.appendChild(div)

您没有将div对象附加到 DOM

document.body.appendChild(div);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM