发布
社区首页 >问答首页 >如何使此样式表与cookie一起工作

如何使此样式表与cookie一起工作
EN

Stack Overflow用户
提问于 2010-02-12 10:49:28
回答 1查看 395关注 0票数 1

我找到了tvanfosson主题插件here。我简化了插件以满足我的需要。它工作得很好。我有一个带有数字选项值的下拉选择列表,用于选择样式表。但是,我需要为每种样式设置一个cookie。cookie已创建,但在保留cookie时遇到问题。修改后的插件如下:

代码语言:javascript
代码运行次数:0
复制
    ;(function($) {

    $.fn.extend({
        retheme: function(options,callback) {
            options = $.extend( {}, $.Retheme.defaults, options );

            this.each(function() {
                new $.Retheme(this,options,callback);
            });
            return this;
        }
    });

    $.Retheme = function( ctl, options, callback ) {
        if (options.styleSheet.match(/^\w/)) {
            options.styleSheet = '#' + options.styleSheet;
        } else {
            //add checking based on url
            options.styleSheet = $('link[href*=' + options.baseUrl + ']');
        }

        $(ctl).filter('select').change( function() {
            loadTheme( themeSelector(this), options );
            if (callback) callback.apply(this);
            return false;
        });
    };

    function themeSelector(ctl) {
        var $this = $(ctl);  
        var theme = $this.attr('href');
        if (!theme) {
            theme = $this.val();
        }

        return theme;
    }

    function loadTheme( theme, options ) {
        //var cookedHref = options.baseUrl + theme;
        //$.cookie('cthemeHref', cookedHref, { expires: 1 });
        //var themeHref = $.cookie('cthemeHref');

        var themeHref = options.baseUrl + theme;

        if (!themeHref.match(/\.css$/)) {
            themeHref += '.css';
        }

        var styleSheet = $(options.styleSheet);

        // set a cookie
        $.cookie('links', styleSheet, {expires: 1, path: '/'});

        var speed = options.speed;
        var delay = options.delay;

        var overlay = buildOverlay();
            overlay.fadeIn( speed, function() {
                styleSheet.attr( 'href', themeHref );
                // set a cookie
                $.cookie('css', themeHref , {expires: 1, path: '/'});
                alert($.cookie('css'));
                //$.get( themeHref, function() { // don't want to keep loading
                   setTimeout( function() {
                     overlay.fadeOut( speed, function() {
                       dieOverlay();
                     });
                   }, delay );
                 //});
               });
    };

    $.Retheme.defaults = {
        baseUrl: '/styles/',
        styleSheet: '',
        speed: 'slow',
        delay: 500
    };

})(jQuery);

然后,我放置以下内容来获取cookie。问题是href属性中的所有链接都被一个选择的cookie替换,而我想为每个选择的链接一次一个地获取或激活cookie。

代码语言:javascript
代码运行次数:0
复制
if($.cookie('css')) {
    //$('link').attr('href', $.cookie('css')); //kill all other hrefs
    $.cookie('links').attr('href', $.cookie('css')); // $.cookie('links') is not a function
} else {
$(function() {
    $('#edit-apple').retheme({baseUrl: '/apple/style-'});
    $('#edit-orange').retheme({baseUrl: '/orange/style-'});
    $('#edit-mango').retheme({baseUrl: '/mango/style-'});
}

注意:在苹果文件夹中,我有样式表style-1.css,style-2.css等。没有其他,它可以工作,但没有cookie抓取。

一如既往,任何帮助都将不胜感激。谢谢

EN

回答 1

Stack Overflow用户

发布于 2010-02-13 08:16:21

存储在$.cookie('css')$.cookie('links')中的值到底是什么

为什么不将css文件名存储在cookie中,然后当页面加载时,检查这个cookie的值。如果cookie存在,并且值是有效的(包含一组有限的允许值),则使用该值并加载样式表文件。如果cookie无效或者还没有任何值,那么加载一个默认的样式表页。

你也可以看看jquery ui是如何实现主题切换器here的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2249369

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档