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>IT研修(xuhuhu.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 對象