Created
March 21, 2012 18:22
-
-
Save ajturner/2150721 to your computer and use it in GitHub Desktop.
Demonstration of changing DOM elements by manipulating CSS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <html> | |
| <head> | |
| <title>Change Color Modes</title> | |
| <style type="text/css"> | |
| body {background-color: #444;} | |
| a, a:visited {color: white;} | |
| ul li { list-style-type: none; display: block; padding: 20px; border: 1px solid #bbb;} | |
| .day { width: 50px; height: 50px; } | |
| </style> | |
| <script type="text/javascript"> | |
| var bins = { | |
| "q1": "blue", | |
| "q2": "white", | |
| "q3": "green" | |
| } | |
| function changeday(day) { | |
| clearStyles() | |
| for(var bin in bins) { | |
| setStyles( "change", ".day_" + day + "_" + bin, {style: "background-color", value: bins[bin]}) | |
| } | |
| } | |
| function clearStyles() { | |
| setStyles("change", ".day_", {style: "background-color", value: ""}); | |
| } | |
| function setStyles(action, match, value) { | |
| var mysheet = document.styleSheets[0] | |
| var myrules = mysheet.cssRules? mysheet.cssRules: mysheet.rules | |
| mysheet.crossdelete=mysheet.deleteRule? mysheet.deleteRule : mysheet.removeRule | |
| var i, found = false; | |
| for (i=0; i<myrules.length; i++){ | |
| if(myrules[i].selectorText.toLowerCase().indexOf(match)!=-1){ | |
| switch(action) { | |
| case "clear": | |
| myrules[i].style[value.style] = ""; | |
| break; | |
| case "change": | |
| myrules[i].style[value.style] = value.value | |
| break; | |
| case "delete": | |
| mysheet.crossdelete(i); | |
| i--; | |
| break; | |
| } | |
| found = true; | |
| } | |
| } | |
| if(!found) | |
| createCSSClass(match, value.style + ":" + value.value) | |
| } | |
| // from http://www.happycode.info/create-css-classes-with-javascript/ | |
| function createCSSClass(selector, style) { | |
| if (!document.styleSheets) { | |
| return; | |
| } | |
| if (document.getElementsByTagName("head").length == 0) { | |
| return; | |
| } | |
| var stylesheet; | |
| var mediaType; | |
| if (document.styleSheets.length > 0) { | |
| for (i = 0; i < document.styleSheets.length; i++) { | |
| if (document.styleSheets[i].disabled) { | |
| continue; | |
| } | |
| var media = document.styleSheets[i].media; | |
| mediaType = typeof media; | |
| if (mediaType == "string") { | |
| if (media == "" || (media.indexOf("screen") != -1)) { | |
| styleSheet = document.styleSheets[i]; | |
| } | |
| } else if (mediaType == "object") { | |
| if (media.mediaText == "" || (media.mediaText.indexOf("screen") != -1)) { | |
| styleSheet = document.styleSheets[i]; | |
| } | |
| } | |
| if (typeof styleSheet != "undefined") { | |
| break; | |
| } | |
| } | |
| } | |
| if (typeof styleSheet == "undefined") { | |
| var styleSheetElement = document.createElement("style"); | |
| styleSheetElement.type = "text/css"; | |
| document.getElementsByTagName("head")[0].appendChild(styleSheetElement); | |
| for (i = 0; i < document.styleSheets.length; i++) { | |
| if (document.styleSheets[i].disabled) { | |
| continue; | |
| } | |
| styleSheet = document.styleSheets[i]; | |
| } | |
| var media = styleSheet.media; | |
| mediaType = typeof media; | |
| } | |
| if (mediaType == "string") { | |
| for (i = 0; i < styleSheet.rules.length; i++) { | |
| if (styleSheet.rules[i].selectorText.toLowerCase() == selector.toLowerCase()) { | |
| styleSheet.rules[i].style.cssText = style; | |
| return; | |
| } | |
| } | |
| styleSheet.addRule(selector, style); | |
| } else if (mediaType == "object") { | |
| for (i = 0; i < styleSheet.cssRules.length; i++) { | |
| if (styleSheet.cssRules[i].selectorText.toLowerCase() == selector.toLowerCase()) { | |
| styleSheet.cssRules[i].style.cssText = style; | |
| return; | |
| } | |
| } | |
| styleSheet.insertRule(selector + "{" + style + "}", 0); | |
| } | |
| } | |
| </script> | |
| </head> | |
| <body> | |
| <ul> | |
| <li class="day day_mon_q1 day_tues_q1 day_wed_q2"></li> | |
| <li class="day day_mon_q1 day_tues_q2 day_wed_q2"></li> | |
| <li class="day day_mon_q1 day_tues_q2 day_wed_q3"></li> | |
| </ul> | |
| <a href="" onclick="changeday('mon'); return false">Monday</a> | |
| <a href="" onclick="changeday('tues'); return false">Tuesday</a> | |
| <a href="" onclick="changeday('wed'); return false">Wednesday</a> | |
| <a href="" onclick="clearStyles(); return false">Clear Styling</a> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment