如何将 CSS 应用于 HTML body 元素? [英] How to apply CSS to HTML body element?

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

问题描述

我正在努力摆脱这个:

document.body.className = "someclass";

我想在 CSS 中做到这一点.

I want to do this in CSS.

body.someclass {
.
.
.
}

不幸的是,我无法在 Firefox、chrome 中使用它.

unfortunately I am not able to get this working in Firefox, chrome.

我在这里做错了什么吗?有什么办法可以将 CSS 类应用于 body 吗?

Am I doing anything wrong here? Is there any way we could apply a CSS class to body?

推荐答案

你在那里做两件不同的事情.您的 JavaScript 实际上将类someclass"分配给您的 body 元素,而您的 CSS 正在样式化一个带有someclass"类的 body 元素,它不会将类分配给它,这不是CSS 的任务.您可以像这样在纯 (X)HTML 中执行此操作:

You are doing two different things there. Your JavaScript is actually assigning the class "someclass" to your body element, while your CSS is styling a body element with the class "someclass", it won't assign the class to it, that is not the task of CSS. You would do it in plain (X)HTML like this:

<body class="someclass">

这篇关于如何将 CSS 应用于 HTML body 元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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