Javascript style.setProperty()方法似乎不起作用

Javascript style.setProperty()方法似乎不起作用,javascript,Javascript,我是stackoverflow的新手,希望能用正确的方式描述我的问题。我想使用setProperty方法向元素添加css样式,但遇到了一些问题。似乎只添加了一些属性,而其他属性没有添加,我不知道为什么。在本例中,它只呈现“background size”属性——当我注释这行代码时,它呈现一个没有任何内联样式的简单div。我会感谢你在这方面的帮助。谢谢这是我的代码: const Img = new Image(); Img.src = "example.jpg"; con

我是stackoverflow的新手,希望能用正确的方式描述我的问题。我想使用setProperty方法向元素添加css样式,但遇到了一些问题。似乎只添加了一些属性,而其他属性没有添加,我不知道为什么。在本例中,它只呈现“background size”属性——当我注释这行代码时,它呈现一个没有任何内联样式的简单div。我会感谢你在这方面的帮助。谢谢这是我的代码:

    const Img = new Image();
    Img.src = "example.jpg";

    const div = document.createElement('div');
    console.log("Simple div: ", div); //<div style="background-size: cover;"></div> ???

    div.style.setProperty('width', 400);
    console.log("Adding width: ", div); //<div style="background-size: cover;"></div>

    div.style.setProperty('height', 300);
    console.log("Adding height", div); //the same as above

    div.style.setProperty('background-image', Img.src);
    console.log("Adding bg image:", div); //the same as above

    div.style.setProperty('background-size', 'cover');
    console.log(div); //<div style="background-size: cover;"></div>

    document.querySelector('body').appendChild(div);
const Img=new Image();
Img.src=“example.jpg”;
const div=document.createElement('div');
console.log(“简单div:,div);/???
div.style.setProperty('width',400);
log(“添加宽度:”,div)//
div.style.setProperty('height',300);
控制台日志(“添加高度”,div)//同上
div.style.setProperty('background-image',Img.src);
log(“添加bg映像:”,div)//同上
div.style.setProperty('background-size','cover');
控制台日志(div)//
document.querySelector('body').appendChild(div);

您在CSS规则中插入了无效值:

在设置元素的
宽度
高度
时,需要指定值的单位

.setProperty('width',400)

应该是

.setProperty('width','400px')

您需要指定作为
背景图像
值传递的内容确实是带有
url(…)
的url

.setProperty('background-image',Img.src)

应该是

.setProperty('background-image','url('+Img.src+'))

const Img=new Image();
Img.src=”https://www.gravatar.com/avatar/c64594c112836c735bf0148a0557795a?s=32&d=identicon&r=PG&f=1";
const div=document.createElement('div');
log(“简单div:”,div);
div.style.setProperty('width','400px');
log(“添加宽度:”,div);
div.style.setProperty('height','300px');
console.log(“添加高度”,div)
setProperty('background-image','url('+Img.src+');
log(“添加bg映像:”,div);
div.style.setProperty('background-size','cover');
控制台日志(div);

document.querySelector('body').appendChild(div)您在CSS规则中插入了无效值:

在设置元素的
宽度
高度
时,需要指定值的单位

.setProperty('width',400)

应该是

.setProperty('width','400px')

您需要指定作为
背景图像
值传递的内容确实是带有
url(…)
的url

.setProperty('background-image',Img.src)

应该是

.setProperty('background-image','url('+Img.src+'))

const Img=new Image();
Img.src=”https://www.gravatar.com/avatar/c64594c112836c735bf0148a0557795a?s=32&d=identicon&r=PG&f=1";
const div=document.createElement('div');
log(“简单div:”,div);
div.style.setProperty('width','400px');
log(“添加宽度:”,div);
div.style.setProperty('height','300px');
console.log(“添加高度”,div)
setProperty('background-image','url('+Img.src+');
log(“添加bg映像:”,div);
div.style.setProperty('background-size','cover');
控制台日志(div);
document.querySelector('body').appendChild(div)使用(点击
F12
)读取任何错误,包括CSS错误。使用(点击
F12
)读取任何错误,包括CSS错误。