Latest web development tutorials

HTML DOM Style backgroundImage 屬性

Style 對象參考手冊 Style對象

定義和用法

backgroundImage 屬性設置或返回元素的背景圖像。

語法

設置backgroundImage 屬性:

Object.style.backgroundImage="url('URL')|none|inherit"

返回backgroundImage 屬性:

Object.style.backgroundImage

描述
url('URL') 图像文件的位置。
none 默认。没有背景图像。
inherit background-image 属性的设置从父元素继承。


瀏覽器支持

Internet ExplorerFirefoxOperaGoogle ChromeSafari

所有主要瀏覽器都支持backgroundImage 屬性。

注意: IE7及更早的版本不支持"inherit"值。IE8 只有規定了!DOCTYPE 才支持"inherit"。 IE9 支持"inherit"。


提示和註釋

提示:除了背景圖片,您也應該指定背景色。如果圖像是不可用的,將使用背景色。


實例

實例

設置背景圖像:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>本教程(w3big.com)</title>
<script>
function displayResult(){
document.body.style.backgroundColor="#f3f3f3";
document.body.style.backgroundImage="url('img_tree.png')";
}
</script>
</head>
<body>

<h1>Hello World!</h1>
<br>
<button type="button" onclick="displayResult()">設置背景圖片</button>

</body>
</html>

嘗試一下»


Style 對象參考手冊 Style對象