我如何参考< HTML>元素的相应DOM对象? [英] How can I reference the <HTML> element's corresponding DOM object?
问题描述
这是您访问< body>
元素以设置背景样式的方式:
document.body.style.background ='';
但是我怎样才能访问< html>
元素以类似的方式?以下不起作用:
document.html.style.background ='';
< html>
元素可以通过 document.documentElement
属性。一般来说,任何文档的根元素都可以通过 .documentElement
。
document.documentElement.style.background ='';
注意: .style.background
返回背景
的值作为嵌入式样式属性。即,使用< html style =background:..>
来定义。如果您想获得计算的样式属性,请使用 getComputedStyle
:
var style = window.getComputedStyle(document.documentElement);
var bgColor = style.backgroundColor;
This is how you access the <body>
element to set a background style:
document.body.style.background = '';
But how can I access the <html>
element in a similar manner? The following doesn't work:
document.html.style.background = '';
The <html>
element can be referred through the document.documentElement
property. In general, the root element of any document can be referred through .documentElement
.
document.documentElement.style.background = '';
Note: .style.background
returns the value for background
as an inline style property. That is, defined using <html style="background:..">
. If you want to get the calculated style property, use getComputedStyle
:
var style = window.getComputedStyle(document.documentElement);
var bgColor = style.backgroundColor;
这篇关于我如何参考< HTML>元素的相应DOM对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!