Javascript dynamic calling of CSS style sheets

Source: Internet
Author: User
Javascript dynamic calling of CSS style sheets (1)

Previously, I often met words such as classname and csstext, but I don't know how to use them. There are four methods to call a style sheet on a webpage.
The first is the external chain, that is, the form of <LINK rel = "stylesheet" href = "/control/CSS/base.css">;
The second is the input style table, and the third is the declaration in the webpage header, such as Finally, the style is written directly after the object, that is, the form of <Div style = "width: 80%...;">. We also need to start from these aspects by using the script call style.
I. In general, we can change the value of href of the external link style to implement real-time switching of the webpage style, that is, "changing the template style ". At this time, we first need to assign an ID to the target to be changed, as shown in figure
<LINK rel = "stylesheet" type = "text/CSS" id = "CSS" href = "firefox.css"/>
It is easy to call, for example, <span on click = "javascript: Document. getelementbyid ('css '). href = 'ie.css'"> click I to change the style </span>
2. Partial style change: Direct style change, classname change, and csstext change. Note: first, JavaScript is very case sensitive. classname cannot write "N" as "N", and csstext cannot write "T" as "T ", otherwise, the effect cannot be achieved. Second, if you change the classname, declare the class in the style sheet in advance.
Document. getelementbyid ('obj '). style. classname = "..." is incorrectly written!
Can only be written as: Document. getelementbyid ('obj '). classname = "..."
However, if csstext is used, style must be added. The correct syntax is as follows:
Document.getelementbyid('obj').style.css text = "..."
I don't have to talk about changing the direct style. You just need to write it into the specific style, as shown in
Document. getelementbyid ('obj '). style. backgroundcolor = "#003366"
New users often do not know how to write CSS styles in Javascript, and sometimes the requirements are different in different browsers. For example, float is written as stylefloat in IE and cssfloat in Firefox. This requires your accumulation.
This article from: the foot home (www.jb51.net) detailed source reference: http://www.jb51.net/article/5161.htm

Javascript dynamic calling of CSS style sheets (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">

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:

  1. 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.
  2. 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>

Related Article

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.