2011-11-14 61 views
1

使用cookies,我想讓它記住頁面的顏色佈局。 (所以,如果他們設置的畫廊一個顏色和車身背景的另一種顏色,它會保存在刷新,但它似乎並不奏效記住頁面的顏色佈局

這裏是我的JavaScript代碼:。

$(document).ready(function() { 

    if (verifier == 1) { 
     $('body').css('background', $.cookie('test_cookie')); 
    } 

    if (verifier == 2) { 
     $('#gallery').css('background', $.cookie('test_cookie')); 
    } 

    if (verifier == 3) { 
     $('body').css('background', $.cookie('test_cookie')); 
     $('#gallery').css('background', $.cookie('test_cookie')); 
    } 

    $('#set_cookie').click(function() { 

     var color = $('#set_cookie').val(); 

     $.cookie('test_cookie', color); 
    }); 

    $('#set_page').click(function() { 
     $('body').css('background', $.cookie('test_cookie')); 
     var verifier = 1; 
    }); 

    $('#set_gallery').click(function() { 
     $('#gallery').css('background', $.cookie('test_cookie')); 
     var verifier = 2; 
    }); 

    $('#set_both').click(function() { 
     $('body').css('background', $.cookie('test_cookie')); 
     $('#gallery').css('background', $.cookie('test_cookie')); 
     var verifier = 3; 
    }); 
}); 

我的HTML:

<body> 
     <div id="wrap"> 
      <div id="header"> 
       <img src="media/header.png" alt="Community Header" /> 
      </div> 

      <p>Please select a background color for either the page's background, the gallery's background, or both.</p> 

      <select id="set_cookie"> 
       <option value="#1d375a" selected="selected">Default</option> 
       <option value="black">Black</option> 
       <option value="blue">Blue</option> 
       <option value="brown">Brown</option> 
       <option value="darkblue">Dark Blue</option> 
       <option value="darkgreen">Dark Green</option> 
       <option value="darkred">Dark Red</option> 
       <option value="fuchsia">Fuchsia</option> 
       <option value="green">Green</option> 
       <option value="grey">Grey</option> 
       <option value="#d3d3d3">Light Grey</option> 
       <option value="#32cd32">Lime Green</option> 
       <option value="#f8b040">Macaroni</option> 
       <option value="#ff7300">Orange</option> 
       <option value="pink">Pink</option> 
       <option value="purple">Purple</option> 
       <option value="red">Red</option> 
       <option value="#0fcce0">Turquoise</option> 
       <option value="white">White</option> 
       <option value="yellow">Yellow</option> 
      </select> 

      <input type="button" id="set_page" value="Page's Background" /><input type="button" id="set_gallery" value="Gallery's Background" /><input type="button" id="set_both" value="Both" /> 


      </div> 
     </div> 
    </body> 

</html> 

下面是一個例子的jsfiddle:http://jsfiddle.net/hL6Ye/

+5

如果您嘗試使用更具描述性的問題標題和reduc,則您有更好的機會獲得答案e你的片段中無關代碼的數量... – bobbymcr

+0

@bobbymcr好吧謝謝,我會整理它。 –

+0

您對「驗證者」的使用讓我感到困惑。 –

回答

2

你的問題是

if (verifier == 2) { 
      $('#gallery').css('background', $.cookie('test_cookie')); 
     } 

$('#set_gallery').click(function() { 
     $('#gallery').css('background', $.cookie('test_cookie')); 
     var verifier = 2; 
    }); 
在你的代碼你的 test_cookie設置背景顏色和上面的 驗證變量2.

從你的代碼中期望

verfier是頁面時加載。這不是事實,JavaScript變量在會話中不是永久性的。如果是的話,我們不需要cookies,我們會

你的方法應該是兩個不同的cookie。 page_background and gallery_background。加載頁面時,應檢查這些cookie的值並相應地設置顏色。如果沒有設置cookie,用戶就不會費心去保存它們。

0

這裏充分的解決方案:http://zequinha-bsb.int-domains.com/index.html

我不能讓它在jsfiddle.net

在這裏工作是它(太多了,也許)的糊狀:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 

<script type='text/javascript' src='jquery-1.5.1.min.js'></script> 
<script type='text/javascript'> 

/* http://www.quirksmode.org/js/cookies.html */ 
function createCookie(name,value,days) { 
    if (days) { 
     var date = new Date(); 
     date.setTime(date.getTime()+(days*24*60*60*1000)); 
     var expires = "; expires="+date.toGMTString(); 
    } 
    else var expires = ""; 
    document.cookie = name+"="+value+expires+"; path=/"; 
} 

function readCookie(name) { 
    var nameEQ = name + "="; 
    var ca = document.cookie.split(';'); 
    for(var i=0;i < ca.length;i++) { 
     var c = ca[i]; 
     while (c.charAt(0)==' ') c = c.substring(1,c.length); 
     if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); 
    } 
    return null; 
} 

function eraseCookie(name) { 
    var date = new Date(); 
    date.setTime(date.getTime()+(-1)); 
    var expires = "; expires="+date.toGMTString(); 
    document.cookie = name+"="+""+expires+"; path=/"; 
} 
/************************** quirksmode.org *****/ 

function setBGColor() { 
    alert('Background color set!'); 
    var theBGColor = $("#setCookie").val(); 
    $('body').css('background-color',theBGColor); 
    createCookie('MYSITEPGBG',theBGColor,365); 
} 

function setGLColor() { 
    alert('Gallery background color set!'); 
    var theGLColor = $("#setCookie").val(); 
    $('#gallery').css('background-color',theGLColor); 
    createCookie('MYSITEGLBG',theGLColor,365); 
} 

$(document).ready(function() { 
    var bgCookie = readCookie('MYSITEPGBG'); 
    var glCookie = readCookie('MYSITEGLBG'); 
    var bgVerifier = true; 
    var glVerifier = true; 

    if (bgCookie != undefined) { 
     bgVerifier = false; 
     $('body').css('background-color',bgCookie); 
    } 
    if (glCookie != undefined) { 
     glVerifier = false; 
     $('#gallery').css('background-color',glCookie); 
    } 
    if (bgVerifier || glVerifier) 
     $('#giveChoices').toggle() 
    else $('#allowChange').toggle(); 
}); 

</head> 

<body> 
<div id='gallery' style='float:left; width:400px; height:200px; display:block; ' > 
    blah blah clah blah blah blah albh 
</div> 
<div id='giveChoices' style='display:none; ' > 
    <p style='clear:both; margin-top:20px; ' > 
     Please select a background color for either the page's background, the gallery's background, or both. 
    </p> 
    <select id="setCookie"> 
     <option value="#1d375a" selected="selected">Default</option> 
     <option value="black">Black</option> 
     <option value="blue">Blue</option> 
     <option value="brown">Brown</option> 
     <option value="darkblue">Dark Blue</option> 
     <option value="darkgreen">Dark Green</option> 
     <option value="darkred">Dark Red</option> 
     <option value="fuchsia">Fuchsia</option> 
     <option value="green">Green</option> 
     <option value="grey">Grey</option> 
     <option value="#d3d3d3">Light Grey</option> 
     <option value="#32cd32">Lime Green</option> 
     <option value="#f8b040">Macaroni</option> 
     <option value="#ff7300">Orange</option> 
     <option value="pink">Pink</option> 
     <option value="purple">Purple</option> 
     <option value="red">Red</option> 
     <option value="#0fcce0">Turquoise</option> 
     <option value="white">White</option> 
     <option value="yellow">Yellow</option> 
    </select> 
    <input type='button' onclick='setBGColor(); ' value="Page's Background" /> 
    <input type='button' onclick='setGLColor(); ' value="Gallery's Background" /> 
    <input type="button" onclick='setBGColor(); setGLColor(); ' value="Both" /> 
</div> 
<div id='allowChange' style='clear:both; float:left; display:none; '> 
<input type='button' onclick="$('#allowChange').fadeOut('slow'); $('#giveChoices').fadeIn('slow'); " value='Change Colors' /> 
</div> 
</body> 
</html>