当用户点击按钮以调用高对比度时,页面的样式会更改并存储在Cookie中。直到这里,好的。但是,从现在开始我想根据页面样式更改同一按钮的标题(打开高对比度/关闭高对比度),它甚至可以在同一页面中工作,但是如果我重新加载页面,标题不保留,只有风格。
我该怎么做才能解决这个问题?我感谢,从现在开始! =]
function getCookie(strCookie){
var strName = strCookie + "=";
var arrCookies = document.cookie.split(';');
for (var i = 0; i < arrCookies.length; i++) {
var strValorCookie = arrCookies[i];
while (strValorCookie.charAt(0) == ' ') {strValorCookie = strValorCookie.substring(1, strValorCookie.length);}
if (strValorCookie.indexOf(strName) == 0) {return strValorCookie.substring(strName.length, strValorCookie.length);}
} return '';
}
function setCookie(name, value, expires, path, domain, secure) {
var today = new Date(); today.setTime( today.getTime() ); expires = 365; path = '/';
if ( expires ) {expires = expires * 1000 * 60 * 60 * 24;}
var expires_date = new Date( today.getTime() + (expires) );
document.cookie = name + "=" +escape( value ) + ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + ( ( path ) ? ";path=" + path : "" ) + ( ( domain ) ? ";domain=" + domain : "" ) + ( ( secure ) ? ";secure" : "" );
}
function eraseCookie(name) {setCookie(name, '', -1);}
$(document).ready(function() {
var cookieContrast = getCookie("escuro");
if (cookieContrast == "escuro"){$("body").addClass("escuro");}
$(".bt-tema").click(function(){
var x = getCookie("escuro");
if (x == "escuro") {$("body").removeClass("escuro"); eraseCookie("escuro");} else {$("body").addClass("escuro"); setCookie("escuro", "escuro");}
此标题会根据网页的样式进行更改,但它并未存储在Cookie中:
var title = 'Ativar o alto contraste (Alt+5)' ; if( $("body").hasClass('escuro')){title = 'Desativar o alto contraste (Alt+5)';} $(this).attr('title', title);
return (false);
});
});