|
Step 1: Define an ID in the element connecting to the style sheet, for example
<LINK rel = "stylesheet" type = "text/CSS" href = "css1.css" id = "cssid">
The id I define is CSS.
Step 2: Write a JS function with the following code:
<SCRIPT type = "text/JavaScript">
Function Change (){
VaR CSS = Document. getelementbyid ("cssid ");
If (A = 1)
CSS. setattribute ("href", "css1.css ");
If (A = 2)
CSS. setattribute ("href", "css2.css ");
If (A = 3)
CSS. setattribute ("href", "css3.css ");
}
</SCRIPT>
The code of this function can be stored anywhere on the page.
Step 3: Add a function trigger event to change the style sheet connection. The Code is as follows:
<A href = "#" onclick = "Change (1)"> I am the first css1. </a>
<A href = "#" onclick = "Change (2)"> I am the second css2 </a>
<A href = "#" onclick = "Change (3)"> I am the third css3. </a>
This effect has been tested in IE and ff. I believe it is very clear after reading it. With this method, we can allow the viewer to select the style sheet to be displayed, for example, you can select a style sheet with a large font. Note the following two points:
- In this example, the name of a function cannot be links or link. If it is links or link, the style sheet will not be changed. I am not sure about the specific reason, it may be a reserved character of JavaScript.
- In addition, to change the style of the entire page, you need to define the height of the body as 100% in the style sheet file.
Method 2
Step 1: Define an ID in the element connecting to the style sheet, for example
<LINK rel = "stylesheet" type = "text/CSS" href = "css1.css" id = "cssid" Media = "screen">
Step 2: Write a JS function with the following code:
<SCRIPT type = "text/JavaScript">
Function Change1 (){
Document. getelementbyid ("cssid"). href = "css1.css ";
}
Function Change2 (){
Document. getelementbyid ("cssid"). href = "css2.css ";
}
Function Change3 (){
Document. getelementbyid ("cssid"). href = "css3.css ";
}
</SCRIPT>
Step 3: Add a function trigger event to change the style sheet connection. The Code is as follows:
<A href = "#" onclick = "Change1 ()"> I am the first css1. </a>
<A href = "#" onclick = "Change2 ()"> I am the second css2 </a>
<A href = "#" onclick = "Change3 ()"> I am the third css3. </a>
Method 3:
<SCRIPT type = "text/JavaScript">
Function loadjscssfile (filename, filetype ){
If (filetype = "JS") {// determine the file type
VaR fileref = Document. createelement ('script') // create a tag
Fileref. setattribute ("type", "text/JavaScript") // defines the attribute type value as text/JavaScript
Fileref. setattribute ("src", filename) // file address
}
Else if (filetype = "CSS") {// determine the file type
VaR fileref = Document. createelement ("Link ")
Fileref. setattribute ("rel", "stylesheet ")
Fileref. setattribute ("type", "text/CSS ")
Fileref. setattribute ("href", filename)
}
If (typeof fileref! = "Undefined ")
Document. getelementsbytagname ("head") [0]. appendchild (fileref)
}
// Loadjscssfile ("myscript. js", "JS") // when the page is opened, the browser dynamically loads the file
Loadjscssfile ("CSS/shop.css", "CSS ")
VaR RL = <% = (string) viewstate ["col"] %>;
VaR K = "";
Switch (RL)
{
Case 1:
K = "1.css ";
Break;
Case 2:
K = "2.css ";
Break;
}
Loadjscssfile (k, "CSS") // when the page is opened, the browser's dynamic .css File
VaR filesadded = "" // Save the array variable of the name of the bound File
Function checkloadjscssfile (filename, filetype ){
If (filesadded. indexof ("[" + filename + "]") =-1) {// indexof checks whether an item exists in the array.
Loadjscssfile (filename, filetype)
Filesadded + = "[" + filename + "]" // Add the file name to filesadded
}
Else
Alert ("file already added! ") // Prompt if it already exists
}
</SCRIPT>
|