I am trying to get rid of this:
我试图摆脱这个:
document.body.className = "someclass";
I want to do this in CSS.
我想在CSS中这样做。
body.someclass {
.
.
.
}
unfortunately I am not able to get this working in Firefox, chrome.
不幸的是,我无法在Firefox,Chrome中使用它。
Am I doing anything wrong here? Is there any way we could apply a CSS class to body?
我在这做错了吗?我们有什么方法可以将CSS类应用于body?
3 个解决方案
#1
14
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:
你在做两件事。您的JavaScript实际上是将“someclass”类分配给您的body元素,而您的CSS使用类“someclass”为body元素设置样式,它不会将类分配给它,这不是CSS的任务。你可以在普通(X)HTML中这样做:
<body class="someclass">
#2
7
Part of the problem is that valid XHTML can only have one <body>
element per document. A class is generally ascribed to an element that occurs multiple times (or, rather, to a style that you want applied multiple times).
部分问题是有效的XHTML每个文档只能有一个元素。类通常归因于多次出现的元素(或者更确切地说,是要多次应用的样式)。
SINCE there's only ever one body, I've never added an ID or a class to it. Just style:
由于只有一个身体,我从未添加过身份证或课程。只是风格:
body {
background-color: red;
}
If you have a class you want to reuse, use a comma in your selector:
如果您有要重用的类,请在选择器中使用逗号:
.coolStyle, body {
font-weight: bold;
color: red;
text-decoration: blink;
}
All of this is potentially meaningless, however, because anything applied to the <body>
tag will be inherited by its children unless explicitly styled otherwise.
但是,所有这些都可能毫无意义,因为除非另外显式设置,否则应用于标记的任何内容都将由其子项继承。
#3
1
There is no need for a class on body since you will only have one body in your document. Unless maybe you want to change the style of the body by changing with JavaScript class for some reason.
因为您的文档中只有一个正文,所以不需要在正文中使用类。除非你想因为某种原因改变JavaScript类来改变身体的风格。
If you have to put a class on body do it like this:
如果你必须在课堂上放一个课,那就这样:
<body class="someclass">
And then access the class and modify the style in your css like this:
然后访问类并修改css中的样式,如下所示:
body.someclass{
...(style parameters goes here)
}
However you can change the style of all elements of a tag and since you will only have one body you could simply add this to the css:
但是,您可以更改标签的所有元素的样式,因为您只有一个主体,您只需将其添加到css:
body{
...(style parameters)
}
And not add anything to the html. However with reoccuring tags do use class for a style that will be used several times and id for a style that will only be used one time on a page. You assign an id to a tag like this in the html:
并没有添加任何东西到HTML。但是,对于reoccuring标记,请使用类作为将多次使用的样式,并将id用于仅在页面上使用一次的样式。你在html中为这样的标签分配了一个id:
<tag id="someid">
#1
14
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:
你在做两件事。您的JavaScript实际上是将“someclass”类分配给您的body元素,而您的CSS使用类“someclass”为body元素设置样式,它不会将类分配给它,这不是CSS的任务。你可以在普通(X)HTML中这样做:
<body class="someclass">
#2
7
Part of the problem is that valid XHTML can only have one <body>
element per document. A class is generally ascribed to an element that occurs multiple times (or, rather, to a style that you want applied multiple times).
部分问题是有效的XHTML每个文档只能有一个元素。类通常归因于多次出现的元素(或者更确切地说,是要多次应用的样式)。
SINCE there's only ever one body, I've never added an ID or a class to it. Just style:
由于只有一个身体,我从未添加过身份证或课程。只是风格:
body {
background-color: red;
}
If you have a class you want to reuse, use a comma in your selector:
如果您有要重用的类,请在选择器中使用逗号:
.coolStyle, body {
font-weight: bold;
color: red;
text-decoration: blink;
}
All of this is potentially meaningless, however, because anything applied to the <body>
tag will be inherited by its children unless explicitly styled otherwise.
但是,所有这些都可能毫无意义,因为除非另外显式设置,否则应用于标记的任何内容都将由其子项继承。
#3
1
There is no need for a class on body since you will only have one body in your document. Unless maybe you want to change the style of the body by changing with JavaScript class for some reason.
因为您的文档中只有一个正文,所以不需要在正文中使用类。除非你想因为某种原因改变JavaScript类来改变身体的风格。
If you have to put a class on body do it like this:
如果你必须在课堂上放一个课,那就这样:
<body class="someclass">
And then access the class and modify the style in your css like this:
然后访问类并修改css中的样式,如下所示:
body.someclass{
...(style parameters goes here)
}
However you can change the style of all elements of a tag and since you will only have one body you could simply add this to the css:
但是,您可以更改标签的所有元素的样式,因为您只有一个主体,您只需将其添加到css:
body{
...(style parameters)
}
And not add anything to the html. However with reoccuring tags do use class for a style that will be used several times and id for a style that will only be used one time on a page. You assign an id to a tag like this in the html:
并没有添加任何东西到HTML。但是,对于reoccuring标记,请使用类作为将多次使用的样式,并将id用于仅在页面上使用一次的样式。你在html中为这样的标签分配了一个id:
<tag id="someid">