JavaScript access control external CSS and browser version judgment

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.