0

クリック時にdivのスタイルをlocalStorageに保存してクリアする機能があります:

var originalAttributes = $('.aaa').attr('style');
$('.aaa').each(function(){
    var d = $(this),
    id = d.attr('id'),
    storedStyle = window.localStorage.getItem('aaaStyle' + id);
    if (storedStyle != undefined){   //style stored
        d.attr('style', storedStyle);
    }
});

//mouse event functions for class="aaa"

$('#save').click(function () {
    $('.aaa').each(function(){
        var d = $(this),
        id = d.attr('id'),
        style = d.attr('style');
        if (style != originalAttributes){   //style changed
            //$.cookie('aaaStyle' + id, style, { expires: 30 });
            window.localStorage.setItem('aaaStyle' + id, style);
        }
    });

});

$('#clear').click(function () {
    // unset changes
    $('.aaa').attr('style',originalAttributes).each(function(){
        var d = $(this),
        id = d.attr('id');
        window.localStorage.removeItem('aaaStyle' + id);
    });
});

http://jsfiddle.net/z8KuE/33/

ローカル ストレージのデータを圧縮するには、このコードに何を追加する必要がありますか?

(各ブラウザにはドメインごとのメモリ制限があり、この機能を最大限に活用したいと考えています)

4

1 に答える 1

1

webworker で文字列を圧縮して、プライマリ スレッドに不要な負荷をかけないようにすることができます。私が知る限り、文字列圧縮用のライブラリがあり、lzma も含まれますサイト - 追加の圧縮 - ID とクラスが 255 未満の場合、ID とクラスのマップを作成し、String.fromCharCode(int) を使用してそれらを 1 文字として保存し、localStorage に 1 つの大きな文字列として保存してからデコードすることができます - inputstr[pos ].charCodeAt(0) は、ページの起動時に使用可能なオブジェクトに変換するすべてのペアに対して。ただし、変更のたびにこの大きな文字列を生成するのはお勧めできません。ページを閉じる前に onbeforeunload イベントを使用してこれを行います。

于 2013-10-01T20:18:01.233 に答える