Implement multiple CSS styles on a page using JS

Source: Internet
Author: User

Step 1: Define an ID in the element connecting to the style sheet, for example CopyCode The Code is as follows: <link href = "1.css" rel =" stylesheet "type =" text/CSS "id =" CSS ">,

The id I define is CSS.

Step 2: Write a JS function with the following code:

Copy code The Code is as follows: <SCRIPT type = "text/JavaScript">
Function Change (){
VaR CSS = Document. getelementbyid ("CSS ");
If (A = 1)
CSS. setattribute ("href", "1.css ");
If (A = 2)
CSS. setattribute ("href", "2.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:

Copy code The Code is as follows: <a href = "#" onclick = "Change (1)"> 1.css</A>
<A href = "#" onclick = "Change (2)"> 2.css</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 after several functions 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. If you want to change the style of the entire page, you need to define the height of the body as 100% in the style sheet file.

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.