HTML DOM background 属性

定义和用法

background 属性在一个声明中设置所有的背景属性。

语法:

  1. Object.style.background=background-color background-image
  2. background-repeat background-attachment background-position
参数 描述
background-color 设置元素的背景色。 - color-name - color-rgb - color-hex - transparent
background-image 设置背景图像。 - url(URL) - none
background-repeat 设置背景图像是否及如何重复。 - repeat - repeat-x - repeat-y - no-repeat
background-attachment 背景图像是否固定或者随着页面的其余部分滚动。 - scroll - fixed
background-position 设置背景图像的起始位置。 - top left - top center - top right - center left - center center - center right - bottom left - bottom center - bottom right - x% y% - xpos ypos

实例

下面的例子改变了文档背景的样式:

  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. function setStyle()
  5. {
  6. document.body.style.background="#FFCC80 url(bgdesert.jpg) repeat-y";
  7. }
  8. </script>
  9. </head>
  10. <body>
  11.  
  12. <input type="button" onclick="setStyle()"
  13. value="Set background style" />
  14.  
  15. </body>
  16. </html>