开发者

jQuery Cookie Setting Attributes

I would like to add two properties to the body tag from two different cookies.

One to change the text size and the other to alter the background image displayed.

The script below works but when I add the background cookie to the script it overwrites the text size style set.

Is it possible to add more than one property to the body tag on document.ready or is there a better way to do this?

$(document).ready(function() {
    if (($.cookie('t开发者_C百科ext-sizer') != null))   {
        $('body').attr("style",$.cookie('text-sizer'));
    }
});


Instead of directly accessing (and overwriting) the 'style' attribute, use instead the css method.

Your code would look like this then (assuming you just had a pt/px value in the 'text-sizer' cookie):

$('body').css("fontSize",$.cookie('text-sizer'));


Can you concatenate the 2 cookies first and then set the the style to body?

something like:

style = $.cookie('text-sizer') + $.cookie('background');
 $('body').attr("style",style);


You're better off adding or removing class names and using CSS.


If you're using the style attribute of the body tag to display the background image as well, then it's not surprising that it overrides the font size style you set earlier. If you're married to in-line styles for whatever reason, you'll want to generate one string for all of your style attribute properties and set that after looking at all of your cookies.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜