Js enables css switching between two sets of single html pages

Source: Internet
Author: User

Step 1: import two sets of css files

[Html]
<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/lele2.css"/>

<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/lele2.css"/> Step 2: Write js functions for switching

[Html]
<Script type = "text/javascript">
 
Var title = "style ";
 
Function setStyle (){

// Only switch between style A and style B
If (title = "style "){
Title = "style B ";
} Else {
Title = "style ";
}

Var I, links;
// Use the dom method to retrieve all link elements
Links = document. getElementsByTagName ("link ");
// Determine whether each link element contains a style string to determine whether the link element is a style table link and whether the link contains the title attribute.
For (I = 0; links [I]; I ++ ){
If (links [I]. getAttribute ("rel"). indexOf ("style ")! =-1 & links [I]. getAttribute ("title ")){
// Set all links to disabled
Links [I]. disabled = true;
// Determine whether a specified title string exists in the title. Set the current link to visible to activate the current link.
If (links [I]. getAttribute ("title"). indexOf (title )! =-1 ){
Links [I]. disabled = false;
// Alert ("OK ");
}

}
}
}
 
</Script>

<Script type = "text/javascript">

Var title = "style ";

Function setStyle (){
 
// Only switch between style A and style B
If (title = "style "){
Title = "style B ";
} Else {
Title = "style ";
}
 
Var I, links;
// Use the dom method to retrieve all link elements
Links = document. getElementsByTagName ("link ");
// Determine whether each link element contains a style string to determine whether the link element is a style table link and whether the link contains the title attribute.
For (I = 0; links [I]; I ++ ){
If (links [I]. getAttribute ("rel"). indexOf ("style ")! =-1 & links [I]. getAttribute ("title ")){
// Set all links to disabled
Links [I]. disabled = true;
// Determine whether a specified title string exists in the title. Set the current link to visible to activate the current link.
If (links [I]. getAttribute ("title"). indexOf (title )! =-1 ){
Links [I]. disabled = false;
// Alert ("OK ");
}

}
}
}

</Script> Step 3: Call the switched js function in the html Tag.

[Html]
<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>

<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>
 

 

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.