1

好的,所以我有这段代码可以操作某个区域以在单击链接时显示不同的内容框。我希望能够添加 cookie,使我能够保持所选内容从一页显示到下一页。

 /* code written by kismet of RPG-Directory.com */
$(function () {
    function selectTab(tab) {
        var i = 1;
        while ($('#tab' + i).length) {
            if (tab.attr('id') !== 'tab' + i) {
                $('#tab' + i).removeClass('highlight');
                $('#tab' + i + '-content').hide();
            }
            else {
                tab.addClass('highlight');
                $('#tab' + i + '-content').show();
            }
            i++;
        }
    }

    $('#tab1').addClass('highlight');
    $('.clickable').each(function () {
        $(this).click(function () {
            selectTab($(this));
        });
    });
});

我意识到这可能最好使用外部文件来完成。我希望原始代码不是我的这一事实不会造成问题。

4

3 回答 3

0

除非您需要支持 IE7,否则您可能要考虑使用localStorage而不是 cookie。使用起来要容易得多:

localStorage.currentTab = i;

i = localStorage.currentTab;
于 2013-07-12T09:26:37.753 回答
0

简单的:

document.cookie = encodeURI("helloworld") + "=" + encodeURI("from console");

你也可以这样做:

document.cookie = "hello=world"
于 2013-07-12T07:14:41.790 回答
0

您可以通过https://github.com/carhartl/jquery-cookie库使用 jquery 设置 cookie。

于 2013-07-12T06:57:45.943 回答