方法一:
預先設置好一個樣式,當用到的時候,只需要給這個對象添加一個類名就行了。
eg:
css中:
.bj { background: url(star.jpg) no-repeat center; background-size: cover; }
js中: oBox.className = "bj";
方法二:
直接使用JS的元素屬性style
1 oBox.style.backgroundImage = "url(star.jpg)";//設置背景圖的的地址
2 oBox.style.backgroundRepeat = "no-repeat";//設置背景不平鋪
3 oBox.style.backgroundPosition = "center";//設置背景圖的位置
4 oBox.style.backgroundSize = "cover";//設置背景圖像的尺寸
