Currently, there are two methods to achieve dynamic loading of CSS:
I. js
Reference http://blog.csdn.net/qsdnet/archive/2006/12/31/1470891.aspx
The main content is as follows:
First, it is generally used to load necessary files in external CSS files.
ProgramCode @ Import url(style.css );
/* It can only be used in CSS files or style labels */
Type 2: simply load an external CSS file in the page
Program code Document. createstylesheet (cssfile );
Third: Use the createelement method to create CSS link labels
Program code VaR head = Document. getelementsbytagname ('head'). Item (0 );
VaR style = Document. createelement ('link ');
Style. href = 'style.css ';
Style. rel = 'stylesheet ';
Style. type = 'text/CSS ';
Head. appendchild (style );
Here are some of the functions I used in my previous projects. I hope they will be useful to you!
Program code Function loadjs (File ){
VaR scripttag = Document. getelementbyid ('loadscript ');
VaR head = Document. getelementsbytagname ('head'). Item (0 );
If (scripttag) head. removechild (scripttag );
Script = Document. createelement ('script ');
Script. src = "../JS/MI _" + file + ". js ";
Script. type = 'text/JavaScript ';
Script. ID = 'loadscript ';
Head. appendchild (SCRIPT );
}
Function loadcss (File ){
VaR csstag = Document. getelementbyid ('loadcss ');
VaR head = Document. getelementsbytagname ('head'). Item (0 );
If (csstag) head. removechild (csstag );
CSS = Document. createelement ('link ');
CSS. href = "../CSS/MI _" + file + ". CSS ";
CSS. rel = 'stylesheet ';
CSS. type = 'text/CSS ';
CSS. ID = 'loadcss ';
Head. appendchild (CSS );
}
Ii. server-side variable loading
There are also two methods:
(1) directly use variables
Code
< Head Runat = "Server" >
< Title > </ Title >
< Script Language = "JavaScript" SRC = "<% = URL %>/JS/jquery. js" > </ Script >
< Script Language = "JavaScript" SRC = "<% = URL %>/JS/jquery-impromptu.2.7.min.js" > </ Script >
< Link Type = "Text/CSS" REL = "Stylesheet" Href = '<% = URL % > /CSS/css.css '/>
< Link Type = "Text/CSS" REL = "Stylesheet" Href = "<% = Newurl %>/CSS/css.css" />
</ Head >
(2) UseLink's runat = "server" attribute to make it a Server Control
< Link ID = "Mypagecsspath" Runat = "Server" REL = "Stylesheet" Type = "Text/CSS" />
Background code:
Protected Htmlgenericcontrol mypagecsspath;
Mypagecsspath. attributes [ " Href " ] = " . " ;