jQuery div在飞行中与背景颜色

Dan*_*iel 3 html jquery properties background-color

我想知道是否有人可以解释为什么在第一个创建的div中背景颜色不起作用,它在第二个例子中也是如此.

$("<div/>", {
width:300,
height:400,
backgroundColor:"#425412", //background-color does not work
text: "hello there"
}).appendTo("body");
Run Code Online (Sandbox Code Playgroud)

注意:如果没有background-color属性,将创建div.

// works as defined including background-color
$("<div style='width:300; height:400; background-color:#425412;'>hello there</div>").appendTo("body");
Run Code Online (Sandbox Code Playgroud)

第一种方法有限制吗?

ale*_*lex 5

您需要使该backgroundColor属性成为css对象的键.

$("<div/>", {
    width:300,
    height:400,
    css: {
        backgroundColor:"#425412"
    },
    text: "hello there"
}).appendTo("body");
Run Code Online (Sandbox Code Playgroud)

jsFiddle.