更改html的背景属性,而不是正文



如何更改html的后台属性?我可以更改body元素的背景,但我想操作html";标签";(?(。

操作主体背景的代码

document.body.style = 'background: radial-gradient(circle, rgba(131,170,203,1) 4%, rgba(69,113,148,1) 50%, rgba(39,71,97,1) 100%);';

这些尝试不起作用

document.html.style = 'background: red';
document.style = 'background: red';

我想更改此部件

html {
font-family: Arial, Helvetica, sans-serif;
color: white;
-webkit-user-drag: none;
user-select: none;
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
background: radial-gradient(circle, rgba(131,170,203,1) 4%, rgba(69,113,148,1) 50%, rgba(39,71,97,1) 100%);
}

您需要使用document.documentElement.style来定位html元素。

document.body.style = 'background: radial-gradient(circle, rgba(131,170,203,1) 4%, rgba(69,113,148,1) 50%, rgba(39,71,97,1) 100%);';
document.documentElement.style = 'background: red';
<html>
<body>
<p>test</p>
</body>
</html>

通过html标记中的javascript更改背景试试这个:

document.getElementsByTagName('html')[0].style.background="radial-gradient(circle, rgba(131,170,203,1) 4%, rgba(69,113,148,1) 50%, rgba(39,71,97,1) 100%)";

只需在DOM中放置一个背景div并对其进行操作。e.G.位置:绝对/z指数:-1

最新更新