javascript document.body.style.backgroundImage 与 document.body.background

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3561760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 01:31:23  来源:igfitidea点击:

document.body.style.backgroundImage vs document.body.background

javascripthtml

提问by Ryan Sampson

What is the difference between the two?

两者有什么区别?

回答by irishbuzz

document.body.backgroundrefers to the deprecated background attribute of the body tag

document.body.background是指已弃用的 body 标签背景属性

http://www.w3schools.com/TAGS/att_body_background.asp

http://www.w3schools.com/TAGS/att_body_background.asp

document.body.style.backgroundImagerefers to the CSS background-image property of the body tag. It is equivalent to something like...

document.body.style.backgroundImage指的是 body 标签的 CSS background-image 属性。它相当于……

body { 
  background-image:url('paper.gif');
}

Use the latter :-)

使用后者:-)

回答by Peter Ajtai

There's a third important entry to consider in addition to the depracated document.body.backgroundand the way to change the CSS background image with document.body.style.backgroundImage.... You can use document.body.style.backgroundto change all the background properties, including color image and repeat:

除了已弃用的document.body.background和更改 CSS 背景图像的方法之外,还有第三个重要的条目需要考虑document.body.style.backgroundImage......您可以使用它document.body.style.background来更改所有背景属性,包括彩色图像和重复:

document.body.style.background = "red";

or, to set more than one property

或者,设置多个属性

document.body.style.background="#0000FF url(example.jpg) repeat-x";

The above sets a background color, a background image, and sets a horizontal repeat for that image.

以上设置了背景颜色、背景图像,并为该图像设置了水平重复。

This is similar to the CSS:

这类似于 CSS:

body {
    background:#0000FF url('example.jpg') repeat-x; }

( here's a little more info about document.body.style.backgroundat W3Schools... I'm sure there's more complete info elsewhere though )

(这里有更多关于W3Schools 的document.body.style.background 的信息......我相信其他地方还有更完整的信息)

回答by Topera

document.body.background: You are not using CSS.

document.body.background: 你没有使用 CSS。

document.body.style.backgroundImage: you are setting using CSS.

document.body.style.backgroundImage:您正在使用 CSS 进行设置。