In fact, many or most CSS files describe webpages as external CSS. Therefore, when Javascript is required to change CSS
When the dynamic effect occurs, JS has to access external CSS. Next we will discuss the example of JS access to external CSS.
In this example, click the button to trigger the event to change the background color of the DIV. First, check the CSS file.
[Css]
. Style1 {
Width: 400px;
Height: 500px;
Background-color: red;
}
. Style1 {
Width: 400px;
Height: 500px;
Background-color: red;
}
Then the HTML file
[Html]
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Title> test.html </title>
<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
<Meta http-equiv = "description" content = "this is my page">
<Meta http-equiv = "content-type" content = "text/html; charset = UTF-8">
<Script type = "text/javascript">
Function test (eventObj ){
// Obtain all class selectors in mycss.css
// This 0 indicates the first css introduced in the HTML page.
Var cssResult = document. styleSheets [0]. rules;
// Obtain the specified CSS class selector according to the subscript
Var style1 = cssResult [0];
If (eventObj. value = "black "){
Style1.style. backgroundColor = "black ";
} Else {
Style1.style. backgroundColor = "red ";
}
}
Function test1 (){
If (window. XMLHttpRequest ){
If (! Window. ActiveXObject ){
Alert ("Mozilla, Safari ");
} Else
Alert ("IE ");
} Else {
Alert ("IE6 ");
}
}
</Script>
<Link rel = "stylesheet" href = "../css/6.css" type =" text/css "> </link>
</Head>
<Body>
<Div id = "div1" class = "style1"> div1 </div>
<Input type = "button" value = "black" onclick = "test (this);"/>
<Input type = "button" value = "red" onclick = "test (this);"/>
<Br/>
<Input type = "button" value = "check browser version" onclick = "test1 ();"/>
</Body>
</Html>
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Title> test.html </title>
<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
<Meta http-equiv = "description" content = "this is my page">
<Meta http-equiv = "content-type" content = "text/html; charset = UTF-8">
<Script type = "text/javascript">
Function test (eventObj ){
// Obtain all class selectors www.2cto.com in mycss.css.
// This 0 indicates the first css introduced in the HTML page.
Var cssResult = document. styleSheets [0]. rules;
// Obtain the specified CSS class selector according to the subscript
Var style1 = cssResult [0];
If (eventObj. value = "black "){
Style1.style. backgroundColor = "black ";
} Else {
Style1.style. backgroundColor = "red ";
}
}
Function test1 (){
If (window. XMLHttpRequest ){
If (! Window. ActiveXObject ){
Alert ("Mozilla, Safari ");
} Else
Alert ("IE ");
} Else {
Alert ("IE6 ");
}
}
</Script>
<Link rel = "stylesheet" href = "../css/6.css" type =" text/css "> </link>
</Head>
<Body>
<Div id = "div1" class = "style1"> div1 </div>
<Input type = "button" value = "black" onclick = "test (this);"/>
<Input type = "button" value = "red" onclick = "test (this);"/>
<Br/>
<Input type = "button" value = "check browser version" onclick = "test1 ();"/>
</Body>
</Html>
[Html]
Function test (eventObj ){
// Obtain all class selectors in mycss.css
// This 0 indicates the first css introduced in the HTML page.
Var cssResult = document. styleSheets [0]. rules;
// Obtain the specified CSS class selector according to the subscript
Var style1 = cssResult [0];
If (eventObj. value = "black "){
Style1.style. backgroundColor = "black ";
} Else {
Style1.style. backgroundColor = "red ";
}
}
Function test (eventObj ){
// Obtain all class selectors in mycss.css
// This 0 indicates the first css introduced in the HTML page.
Var cssResult = document. styleSheets [0]. rules;
// Obtain the specified CSS class selector according to the subscript
Var style1 = cssResult [0];
If (eventObj. value = "black "){
Style1.style. backgroundColor = "black ";
} Else {
Style1.style. backgroundColor = "red ";
}
} This function indicates that the meaning of the function has been introduced. It should be said that it is a good way to access external CSS. Of course, browser compatibility is required.
Judge the browser version. The test1 () function illustrates this by using the spatial support of ActiveX windows. In fact, it should be more comprehensive.
From a352193394