我只想使用 JavaScript 创建侧边栏,但无法将关闭按钮添加到 div。 我试过这个:
var sideb = document.createElement("Div");
sideb.style.height = "100%";
sideb.style.width = "0px";
sideb.style.backgroundColor = "rgb(30,30,30)";
sideb.style.top = "0px";
sideb.style.right = "0px";
sideb.style.margin = "0px";
sideb.style.padding = "0px";
sideb.style.position = "fixed"
document.body.appendChild(sideb);
var close = document.createElement("Button");
close.innerText = "close";
close.onclick = close;
document.body.sideb.appendChild(close)
但它不起作用。
最佳答案
document.body.sideb.appendChild(close)
body
没有名为 sideb
你只需要
sideb.appendChild(close)
关于javascript - JS - 如何使用 appendChild 将按钮添加到 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44948388/