0

我创建了一个响应式设计,允许用户在移动和桌面布局之间切换。我唯一的问题是,如果用户想要查看桌面版本,然后单击另一个链接,或刷新页面,则再次加载移动版本。我想创建一个 cookie,以便它记住用户在每个页面加载时的偏好。

这是我正在使用的javascript:

$(document).ready(function () {
    $('.full_site').click(function() {
        $('body').removeClass('mobile');
        $('body').addClass('desktop');
        $('.views-row').each(function(i,e) {
            $('.content .body', e).insertAfter($('.content .loc-text', e));
        });
    });

    $('.mobile_site').click(function() {
        $('body').removeClass('desktop');
        $('body').addClass('mobile');
        $('.views-row').each(function(i,e) {
          $('.content .body', e).insertBefore($('.content .field-name-field-image-one', e));
        });

        $('.views-row').each(function(i,e) {
          $('.content .body', e).insertBefore($('.content .field-name-field-image', e));
        });
    });
}); 
4

1 回答 1

0

以下是一个粗略的草稿,其中包含可帮助您完成整个过程的评论:

$(document).ready(function () {
    $('.full_site').click(function() {
        document.cookie = "type=full"; //set/update the cookie
        $('body').removeClass('mobile');
        $('body').addClass('desktop');
        $('.views-row').each(function(i,e) {
            $('.content .body', e).insertAfter($('.content .loc-text', e));
        });
    });

    $('.mobile_site').click(function() {
        document.cookie = "type=mobile"; //set/update cookie
        $('body').removeClass('desktop');
        $('body').addClass('mobile');
        $('.views-row').each(function(i,e) {
          $('.content .body', e).insertBefore($('.content .field-name-field-image-one', e));
        });

        $('.views-row').each(function(i,e) {
          $('.content .body', e).insertBefore($('.content .field-name-field-image', e));
        });
    });
   if(/*check to see what the cookie is...*/) 
     $(".mobile_site").trigger("click"); //trigger the mobile site
   else
     $(".full_site").trigger("click"); //trigger the full site
}); 

有关 cookie 的更多信息:https ://developer.mozilla.org/en-US/docs/DOM/document.cookie

和:http ://www.quirksmode.org/js/cookies.html

于 2012-09-13T17:28:51.867 回答