我如何参考< HTML>元素的相应DOM对象? [英] How can I reference the <HTML> element's corresponding DOM object?

查看:126
本文介绍了我如何参考< HTML>元素的相应DOM对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是您访问< 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;

这篇关于我如何参考&lt; HTML&gt;元素的相应DOM对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆