Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JS-如何使用appendChid将按钮添加到div中_Javascript - Fatal编程技术网

Javascript JS-如何使用appendChid将按钮添加到div中

Javascript JS-如何使用appendChid将按钮添加到div中,javascript,Javascript,我想只使用JavaScript创建一个侧栏,但我不能将close按钮添加到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.sty

我想只使用JavaScript创建一个侧栏,但我不能将close按钮添加到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)
主体没有名为sideb的属性/键

你只需要

sideb.appendChild(close)
主体没有名为sideb的属性/键

你只需要

sideb.appendChild(close)

这种形式的document.body.sideb意味着调用一个名为sideb的属性,body对象中没有sideb,所以只需使用sideb.appendChild

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.getElementByTagName('body')[0].appendChild(sideb);
var close = document.createElement("Button");
close.innerText = "close";
close.onclick = close;
sideb.appendChild(close)

这种形式的document.body.sideb意味着调用一个名为sideb的属性,body对象中没有sideb,所以只需使用sideb.appendChild

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.getElementByTagName('body')[0].appendChild(sideb);
var close = document.createElement("Button");
close.innerText = "close";
close.onclick = close;
sideb.appendChild(close)

您的代码中有几个bug。你做了一个叫做close的函数吗?我之所以这样做,是因为您将close分配给按钮的onclick处理程序,但是在您将close定义为本地范围中的按钮元素之后。如果没有,您必须编写一个函数来实际关闭div。另外,正如另一个人指出的,sideb不是document.body的属性。您可以调用sideb的appendChild方法来添加按钮

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 = function() {
    sideB.parentNode.removeChild(sideB);
};

sideb.appendChild(close);
另外,如果要删除一些代码重复,可以使用删除对sideb.style的所有额外引用


您的代码中有几个bug。你做了一个叫做close的函数吗?我之所以这样做,是因为您将close分配给按钮的onclick处理程序,但是在您将close定义为本地范围中的按钮元素之后。如果没有,您必须编写一个函数来实际关闭div。另外,正如另一个人指出的,sideb不是document.body的属性。您可以调用sideb的appendChild方法来添加按钮

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 = function() {
    sideB.parentNode.removeChild(sideB);
};

sideb.appendChild(close);
另外,如果要删除一些代码重复,可以使用删除对sideb.style的所有额外引用


昆汀的评论被解码了。请写几行关于你的答案,而不是仅仅给出工作代码。请写几行关于你的答案,而不是仅仅给出工作代码。