方法一:
预先设置好一个样式, 当用到的时候, 只需要给这个对象添加一个类名就行了.
eg:
CSS 中:
- .bj {
- background: url(star.jpg) no-repeat center;
- background-size: cover;
- }
JS 中:
oBox.className = "bj";
方法二:
直接使用 JS 的元素属性 style
- oBox.style.backgroundImage = "url(star.jpg)";// 设置背景图的的地址
- oBox.style.backgroundRepeat = "no-repeat";// 设置背景不平铺
- oBox.style.backgroundPosition = "center";// 设置背景图的位置
- oBox.style.backgroundSize = "cover";// 设置背景图像的尺寸
来源: http://www.bubuko.com/infodetail-3175535.html