Using JS to implement a page of multiple CSS styles to achieve _javascript skills

Source: Internet
Author: User
Step one: Define an ID in the element that connects the style sheet, for example
Copy Code code as follows:

<link href= "1.css" rel= "stylesheet" type= "Text/css" id= "CSS";

The ID I defined is CSS.

The second step: Write a JS function, the code is as follows:

Copy Code code as follows:

<script type= "Text/javascript" >
function Change (a) {
var Css=document.getelementbyid ("CSS");
if (a==1)
Css.setattribute ("href", "1.css");
if (a==2)
Css.setattribute ("href", "2.css");
}
</script>

This function's code can be placed anywhere on the page.

Step three: Add a function trigger event for the connection that changes the page's style sheet, the code reads as follows:

Copy Code code as follows:

<a href= "#" onclick= "Change (1)" >1.css</a>
<a href= "#" onclick= "Change (2)" >2.css</a>

This effect in IE and FF are tested through, I believe that we read after reading because it should be very clear, using this method we can allow viewers to choose their own display style sheet, such as the elderly can choose a larger font style sheet. The two points to note here are:
First, in this example, the name after the function name can not be links or link, if links or link, the style sheet will not be changed, for any reason I am not clear, may be the reserved character of JavaScript.
Second, if you change the style of the entire page, you need to define the body height to 100% in the stylesheet file.

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.