简体   繁体   English

如何使用JavaScript更改背景图片及其属性

[英]How to change background image and it's properties with javascript

How can I change not only background image but also set additional properties? 我如何不仅可以更改背景图像,还可以设置其他属性?

Meaning, I have this code, that works fine: 意思是,我有这段代码,可以正常工作:

document.getElementById(id).style.backgroundImage = "url('image.png')";

But this fails: 但这失败了:

document.getElementById(id).style.backgroundImage = "url('image.png') no-repeat center center";

How can I add the additional properties to it? 如何向其中添加其他属性?

Thanks in advance. 提前致谢。

其他属性是CSS快捷方式的一部分,可通过background属性应用,因此您的js应该是

document.getElementById(id).style.background = "url('image.png') no-repeat center center";

document.getElementById("div3").style.backgroundPosition = "20% 50%"; - for position -位置

document.getElementById("div3").style.backgroundRepeat = "repeat-y"; - for repeat

This will work for you 这对你有用

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

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