Applying style sheets from JavaScriptQuestion: How do I apply another stylesheet to my document using JavaScript?
Answer: The browser loads stylesheets (CSS files) using one or more <LINK rel="stylesheet" type="text/css" href="styleA.css"> <LINK rel="stylesheet" type="text/css" href="styleB.css"> <LINK rel="stylesheet" type="text/css" href="styleC.css">JavaScript accesses stylesheets using the document.styleSheets collection.
document.styleSheets[i].disabled to false
for the desired stylesheet. disabled to true
Example. The following function enables stylesheet number function applyStyle(k) { if (document.styleSheets) { var nStyles = document.styleSheets.length; for (var i=0;i<nStyles;i++) { if (i==k) document.styleSheets[i].disabled = false; else document.styleSheets[i].disabled = true; } } } Try it in action now: - this invokes applyStyle(0)
- this invokes applyStyle(1)
- this invokes applyStyle(2)
|
Copyright © 1999-2011, JavaScripter.net.