How to dynamically change styles in a Web page

Source: Internet
Author: User
Dynamic | Web page

To achieve dynamic Web pages, there are many scenarios, which through CSS to dynamically change the interface style is the most popular web page designers use the technology.

When you use CSS styles to dynamically change an interface style, you typically define multiple class style objects and then set the object's class attribute in JavaScript (or VBScript) scripts to a specified style class name or directly modify the object's Style property. These methods are very effective at modifying a small number of limited styles, but they can be very cumbersome if you want to change the style of the elements of the entire page.

This provides a way to dynamically modify the interface style by dynamically loading CSS style files:

Method:
Store multiple styles of the interface in a different style file, and then dynamically load the new style file through the client's action (such as pressing the button) to implement the batch modification of the interface element style.

Principle:
Each style style object has a Stylesheet property that points to the style object of that style object.
The Stylesheet object has the Csstext attribute, the value is the concrete setting content of the style text, can read and write, we can set the value of this property to an empty string deletion style.
The Stylesheet object has a AddImport method for loading a URL file in the form of: Stylesheet.addimport (Surl[,iindex]), sURL for the CSS file URL to load. Iindex is the location of the request for the style list in the collection, and the default is to add the style list to the end of the collection.

Example:
This example toggles the page background color dynamically by clicking the button.

HTML code:



<title></title>
<meta name= "generator" content= Microsoft Visual Studio.NET 7.0 >
<link href= "Stylesheet1.css" type= "Text/css" rel= "stylesheet" id= "Mycss" >
<script language= "JavaScript" >
var Turnback=false;
function Setcss ()
{
Mycss.stylesheet.csstext= "";
Mycss.styleSheet.addImport (turnback?) Stylesheet1.css ":" Stylesheet2.css ");
Turnback=!turnback;
}
</script>

<body>
<button type= "button" > Toggle background Color </button>
</body>

STYLESHEET1.CSS Style Code:

Body
{
Background-color:black;
}

STYLESHEET2.CSS Style Code:

Body
{
Background-color:maroon;
}

Posted on 2004



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.