So, as some of the comments say, your best bet is to switch CSS classes. But if you change the style of the whole page, then sharing style sheets seems like a great idea to me. But you don’t need to “restore” elements every time you want to change style sheets. you probably better just separate them from the DOM as needed. So something like this:
$(document).ready(function() { var sheets = { //a map of sheets. Simply add sheets to this to add more themes (and create appropriate links to them) silver: $('<link rel="stylesheet" type="text/css" href="themes/silver/css/template.css"/>'), red: $('<link rel="stylesheet" type="text/css" href="css/template.css"/>') }; var currentSheet = sheets.red.appendTo($("head")); //attach default sheet $("a.swapStyle").click(function () { currentSheet.detach(); //remove the current sheet currentSheet = (sheets[$(this).attr("data-theme")]).appendTo($("head")); //attach a new sheet and set the currentSheet variable to remember it. }); });
You will need to change your links to something like this:
<a href="javascript:void(0)" class="swapStyle" data-theme="red">Change to red theme</a> <a href="javascript:void(0)" class="swapStyle" data-theme="silver">Change to silver theme</a>
source share