I got that solution:
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Cor fundo</title>
<link rel="alternate stylesheet" href="css-teste/escuro.css" title="2">
<link rel="alternate stylesheet" href="css-teste/claro.css" title="1">
<script src="https://code.jquery.com/jquery-1.11.3.js"></script>
<script type="text/javascript" src="js/alterar.js"></script>
</head>
<body>
<button id="click">Contraste</button>
<br><br>
Conteúdo do site
</body>
</html>
Jquery
$(document).ready(function() {
if($.cookie("contrast-bar")) {setActiveStyleSheet($.cookie("contrast-bar"));}
$('#click').click(function(e) {
e.preventDefault();
if ( getActiveStyleSheet() == '1') {
setActiveStyleSheet('2');
} else {
setActiveStyleSheet('1');
}
});
});
function setActiveStyleSheet(title) {
var i, a, main;
for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
a.disabled = true;
if(a.getAttribute("title") == title) a.disabled = false;
}
}
$.cookie("contrast-bar",title, {expires: 365, path: '/'});
}
function getActiveStyleSheet() {
var i, a;
for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
}
return null;
}
function getPreferredStyleSheet() {
var i, a;
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")) return a.getAttribute("title");
}
return null;
}
jQuery.cookie = function(name, value, options) {
if (typeof value != 'undefined') { // name and value given, set cookie
options = options || {};
if (value === null) {
value = '';
options.expires = -1;
}
var expires = '';
if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
var date;
if (typeof options.expires == 'number') {
date = new Date();
date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
} else {
date = options.expires;
}
expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
}
// CAUTION: Needed to parenthesize options.path and options.domain
// in the following expressions, otherwise they evaluate to undefined
// in the packed version for some reason...
var path = options.path ? '; path=' + (options.path) : '';
var domain = options.domain ? '; domain=' + (options.domain) : '';
var secure = options.secure ? '; secure' : '';
document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
} else { // only name given, get cookie
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
};// JavaScript Document
dark css.
body{
background-color: #000;
color: #FFF;
font-family: 'Roboto', sans-serif;
}
clear css.
body{
background-color: #FFF;
color: #00F;
font-family: 'Roboto', sans-serif;
}
Good explanation Renan, I was thinking of putting in my answer, the question code changes the background color of the
document
, but his problem must be all the other elements that remain with their respective style.– Mathiasfc
Hello Renan. Got it. In case I want to use two style sheets. How would I make this switch using just one button?
– user24136
I found a solution on this site, but it uses 02 links. I would like to use only 01. http://www.maujor.com/tutorial/alternatecss.php
– user24136
@Fox.11 I always prefer to make the change in the backend. You need to use something like PHP, or ASP.NET.
– Oralista de Sistemas