2011-08-27 74 views
1

此刻,我已將字體大小功能添加到我的Textarea。使用Cookies更改文本區域的字體大小

它工作正常,但現在我想添加餅乾。 這是我走到這一步:

var size=size2 + "px"; 
var size2=15; 

function getCookie(c_name){var i,x,y,ARRcookies=document.cookie.split(";");for (i=0;i<ARRcookies.length;i++){x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);x=x.replace(/^\s+|\s+$/g,"");if (x==c_name){return unescape(y);}}} 
function setCookie(c_name,value,exdays){var exdate=new Date();exdate.setDate(exdate.getDate() + exdays);var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());document.cookie=c_name + "=" + c_value;} 

var cookiesize=getCookie('fontsize'); 
if(cookiesize!==undefined&&cookiesize!==""&&cookiesize!==null){ 
size2=cookiesize; 
} 
else{} 
function add(){ 
    size2++; 
    size=size2+"px"; 
    if(size2 > 40){size2=40;} 

    setCookie('fontsize',size2,365); 

    $('#current').html("<b>"+size2+"</b>"); 
    $('#curren').stop(true,true).animate({'top' : '0px'},500); 
    $('#curren').stop(true,true).delay(3500).animate({'top' : '-50px'},1500); 
    $('#code1').css('font-size', size2); 
} 
function less(){ 
    size2--; 
    size=size2+"px"; 
    if(size2 < 5){size2=5;} 

    setCookie('fontsize',size2,365); 

    $('#current').html("<b>"+size2+"</b>"); 
    $('#curren').stop(true,true).animate({'top' : '0px'},500); 
    $('#curren').stop(true,true).delay(4000).animate({'top' : '-50px'},1500); 
    $('#code1').css('font-size', size2); 
} 
$(document).ready(function(){ 
    $('#panel').html("<form method='post' name='pad' align='center'><textarea class='look' rows='11' id='code1' name='text' cols='58'></textarea><br></form>"); 
    $('#current').html("<b>"+size2+"</b>"); 
    $('#curren').stop(true,true).animate({'top' : '0px'},500); 
    $('#curren').stop(true,true).delay(4000).animate({'top' : '-50px'},1500); 
    $('#code1').css('font-size', size2); 
}); 
function show(){ 
    $('#curren').stop(true,true).animate({'top' : '0px'},500); 
    $('#curren').stop(true,true).delay(4000).animate({'top' : '-50px'},1500); 
} 

我沒有給你們的HTML或CSS,但它不應該是必要的。 現在我所得到的是字體大小數字應該是的未定義消息。

希望你能幫助!
最好的問候,
肖恩

+0

而且,你需要幫助的是什麼問題?有什麼不工作?哪行代碼會出錯? – jfriend00

回答

0

編輯:我現在看到一個錯誤。從這個切換這兩個語句的順序:

var size=size2 + "px"; 
var size2=15; 

這樣:

var size2=15; 
var size=size2 + "px"; 

你被引用size2它被定義之前。

而且,這裏是一個代碼建議:

更改此:

if(cookiesize!==undefined&&cookiesize!==""&&cookiesize!==null){ 
size2=cookiesize; 
} 
else{} 

這樣:

if (cookiesize) { 
    size2 = cookiesize; 
} 

if (cookiesize)防止nullundefined0false""全部一張支票。我還建議你在你的代碼中使用一些空格和新行,以使其更具可讀性。

+0

在前兩行中發現錯誤,並將其添加到我的答案中。 – jfriend00

相關問題