简体   繁体   English

更改CSS下的属性的Javascript

[英]Javascript to change attribute under CSS

document.getElementById("Id1").style. document.getElementById(“ Id1”)。style。 backgroundImage = "url(value)"; backgroundImage =“ url(value)”;

to change the background image attribute in the CSS. 更改CSS中的背景图片属性。 But, the attribute used in CSS is 但是,CSS中使用的属性是

background-Image 背景图

Why we can't use the same name under Javascript as document.getElementById("Id1").style. 为什么我们不能在Javascript下使用与document.getElementById(“ Id1”)。style相同的名称 background-Image = "url(value)"; background-Image =“ url(value)”;

As @Bitwise Creative said, you can't use - out of string. 正如@Bitwise Creative所说,您不能使用-字符串不足。 Instead you can do this, 相反,您可以这样做,

document.getElementById("Id1").style["background-image"] = "url(value)";

If you use document.getElementById("Id1").style.background-Image = "url(value)", what will happen means javascript consider as statement before '-' "document.getElementById("Id1").style.background" and image consider as variable. 如果您使用document.getElementById(“ Id1”)。style.background-Image =“ url(value)”,将发生的情况意味着javascript将考虑作为'-'“ document.getElementById(” Id1“)。style.background之前的语句”,并且图片被视为变量。 So you will get syntax error 所以你会得到语法错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM