Sam Carleton a écrit :
How do I change the CSS colors via JavaScript DOM? Let me explain...
I am working on a Windows application (in C#) that displays some HTML.
In one place the HTML is a status window. What happens is the static
HTML page is embedded into the application. The static page displayed
and then the C# code gets a hold of the HTML DOM from the web browser
and updates what pieces need to be updated.
What I need to do now is change the colors of everything on the static
page. At present there is an embedded CSS style in the HTML and all
the colors are defined there. Using the DOM, via C# code, how do I
change the colors of everything?
1: Can I simply update the CSS and it will auto magically happen? If
so, how does one update the CSS via the DOM?
2: Do I need to go to each individual item and change the color there?
3: Is there a better way to do this all the way around?
I think best way is to have 2 titled alternate styles sheets embedded in
your files.
Then you'll just have to switch between both (or more).
<link rel="alternate stylesheet" type="text/css"
href="../../css/yellow.css" media="all" title="Yellow">
<link rel="alternate stylesheet" type="text/css"
href="../../css/blue.css" media="all" title="Blue">
Here functions for several alternate styles sheets :
function setActiveStyleSheet(title) {
var i, a, main='';
for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
if(a.getAttribute("rel").indexOf("style") != -1) {
if(a.getAttribute("title")) {
a.disabled = true;
if(a.getAttribute("title") == title)
a.disabled = false;
}
}
}
}
function getActiveStyleSheet() {
var i, a, main = 'Defaut';
for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
if(a.getAttribute("rel").indexOf("style") != -1
&& a.getAttribute("rel").indexOf("alt") != -1
&& a.getAttribute("title")
&& !a.disabled) {
main = a.getAttribute("title");
return main;
}
}
return null;
}