Div没有出现在javascript中?

时间:2015-03-15 06:00:13

标签: javascript jquery html

我正在做odin项目,对于前端webdev项目,我必须使用javascript / jQuery创建一个网格。我尝试使用createElement方法,但我想知道如何在html中显示div? 我是不是错了?

HTML:

<!DOCTYPE html>
    <html>

    <head>
        <title>Etch-a-Sketch</title>
        <link rel="stylesheet" type="text/css" href="etch-a-sketch.css">

        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

        <script type = "text/javascript" src="etch-a-sketch.js"></script>

    </head>

    <body>


    </body>

</html>

JS:

var div = document.createElement("div");
div.style.width = "100px";
div.style.height = "100px";
div.style.color = "blue";

document.body.appendChild(div);

2 个答案:

答案 0 :(得分:2)

添加background-color

var div = document.createElement("div");
div.style.width = "100px";
div.style.height = "100px";
div.style.color = "blue";

div.style.backgroundColor = "yellow";//you forgot background color

document.body.appendChild(div);

答案 1 :(得分:1)

添加更多样式以使其可见,例如边框或背景颜色。

&#13;
&#13;
var div = document.createElement("div");
div.style.width = "100px";
div.style.height = "100px";
div.style.color = "blue";
div.style.backgroundColor = "blue";

document.body.appendChild(div);
&#13;
&#13;
&#13;