Today, the study of JS with the SetAttribute method to achieve a page two style sheet effect, the specific methods are as follows:
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:
In this example, the name behind the function name cannot be links or link, and if it is links or link, the stylesheet will not be changed, and for what reason I am not quite sure, it may be a reserved character of JavaScript.
In addition, if you change the style of the entire page, you need to define the body height to 100% in the stylesheet file.
Method Two:
First step: Import two sets of CSS files
Copy Code code as follows:
<link rel= "stylesheet" type= "Text/css" title= "style a" href= "Css/people1.css"/>
<link rel= "Alternate stylesheet" type= "Text/css" title= "Style B" href= "Css/people2.css"/>
The second step: Write switch JS function
Copy Code code as follows:
<script type= "Text/javascript" >
var title = "Style A";
function SetStyle () {
Just style A and style B toggle
if (title== "style a") {
title = "Style B";
}else{
title = "Style A";
}
var i,links;
Get all link elements with the DOM method
Links = document.getElementsByTagName ("link");
Determines whether each link element contains a style string that is used to determine if this link element is a style sheet link and whether this link contains a title attribute
for (i=0; links[i]; i++) {
if (Links[i].getattribute ("rel"). IndexOf ("style")!=-1 && links[i].getattribute ("title") {
Set all link to disabled
Links[i].disabled = true;
To determine if there is a specified title string in the title, then set the current link to visual activate the current link
if (Links[i].getattribute ("title"). IndexOf (title)!=-1) {
links[i].disabled = false;
Alert ("OK");
}
}
}
}
</script>
Step three: Call the Switched JS function in the HTML tag
Copy Code code as follows:
<a href= "#" onclick= "SetStyle ();" >1</a>
<a href= "#" onclick= "SetStyle ();" >2</a>
<a href= "#" onclick= "SetStyle ();" >3</a>
<a href= "#" onclick= "SetStyle ();" >4</a>
<a href= "#" onclick= "SetStyle ();" >5</a>