function getCookie(name) { var matches = document.cookie.match(new RegExp( "(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)" )); return matches ? decodeURIComponent(matches[1]) : undefined; } $(function(){ var yourcolor = getCookie('color'); if(yourcolor){ if(yourcolor=="button1"){ } else if(yourcolor=="button2"){ $('.button1').removeClass('active'); $('.button2').addClass('active'); } } $('.button1, .button2').click(function(){ $('.button1, .button2').toggleClass('active'); if($('.button1').hasClass('active')){ var date = new Date(new Date().getTime() + 60 * 1000); document.cookie = "color=button1; path=/; expires=" + date.toUTCString(); } else if($('.button2').hasClass('active')){ var date = new Date(new Date().getTime() + 60 * 1000); document.cookie = "color=button2; path=/; expires=" + date.toUTCString(); } }); }); Вот, но не работает. |
window.onload = function() { document.getElementById('SuperButton').onclick = function() { if(document.getElementById('col').className != 'whitefon') { document.getElementById('col').className = 'whitefon'; } else { document.getElementById('col').className = 'topic-content'; } } } function getCookie(name) { var matches = document.cookie.match(new RegExp( "(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)" )); return matches ? decodeURIComponent(matches[1]) : undefined; } $(function(){ var yourcolor = getCookie('color'); // alert(1); if(yourcolor){ if(yourcolor=="button1"){ } else if(yourcolor=="button2"){ $('.button1').removeClass('active'); $('.button2').addClass('active'); } } $('.button1, .button2').click(function(){ $('.button1, .button2').toggleClass('active'); if($('.button1').hasClass('active')){ var date = new Date(new Date().getTime() + 60 * 1000); document.cookie = "color=button1; path=/; expires=" + date.toUTCString(); } else if($('.button2').hasClass('active')){ var date = new Date(new Date().getTime() + 60 * 1000); document.cookie = "color=button2; path=/; expires=" + date.toUTCString(); } }); }); Точнее записывает фон кнопки, а фон самого текста .whitefon нет. |
Цитата:
|
http://codepen.io/warycooper/pen/yVpxNL - Вот здесь полный пример
|
Цитата:
Если я правильно понял, нужно мочь кнопками менять фон и при перезагрузке последний фон должен оставаться? |
Цитата:
|
Цитата:
<!DOCTYPE html> <html> <head> <meta http-equiv='Content-Type' content='text/html; charset=windows-1251' /> <script src='http://code.jquery.com/jquery-latest.js'></script> <!-- <script src="https://code.angularjs.org/1.3.9/angular.min.js"></script> <script src="https://code.angularjs.org/1.3.9/angular-route.js"></script> --> <style type='text/css'> </style> <script type='text/javascript'> $(function(){ var clr=getCookie('color'); if (clr) { $('#test').css('background-color',clr); }; $('#btn1').click(function(){ var clr='#ffffff'; $('#test').css('background-color',clr); setCookie('color',clr); }); $('#btn2').click(function(){ var clr='red'; $('#test').css('background-color',clr); setCookie('color',clr); }); }); function getCookie(name) { var matches = document.cookie.match(new RegExp( "(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)" )); return matches ? decodeURIComponent(matches[1]) : undefined; }; function setCookie(name, value, options) { options = options || {}; var expires = options.expires; if (typeof expires == "number" && expires) { var d = new Date(); d.setTime(d.getTime() + expires * 1000); expires = options.expires = d; }; if (expires && expires.toUTCString) { options.expires = expires.toUTCString(); }; value = encodeURIComponent(value); var updatedCookie = name + "=" + value; for (var propName in options) { updatedCookie += "; " + propName; var propValue = options[propName]; if (propValue !== true) { updatedCookie += "=" + propValue; } }; document.cookie = updatedCookie; } </script> </head> <body> <div> <button id="btn1">сделать белый фон</button> <button id="btn2">сделать тёмный фон</button> </div> <div id="test">Здесь меняет фон текста</div> </body> </html> |
Спасибо за помощь! А как записать, что бы цвет текста тоже менялся?
|
Цитата:
|
Для работы с куками есть замечательный плагин jquey.cookie
В нем есть и полное удаление установленных кук Описание и плагин: https://github.com/carhartl/jquery-cookie |
Часовой пояс GMT +3, время: 21:53. |