0

I am trying to remember open accordions in a custom plugin. The first accordion is open by default, so:

var active = [0];

I click on accordions and then read from localStorage to get the values. Inside my click event:

var active = JSON.parse(localStorage.getItem(outerName)),
    tab = $(this).find('h3').index(ui.tab[0]);

if (tab in active) {
    delete active[tab];
} else {
    active[tab] = tab;
}
for (var i = 0; i < this.length; i++) {
    if (active[i] == null) {
        active.splice(i, 1);
        i--;
    }
}
localStorage.setItem(outerName, JSON.stringify(active));

This works, except where I remove the first accordion and then click the second and I'll get duplicated values [1,1]. I have already removed the null values each time so this is the cause but I don't know how to get the values properly so that if I toggle an accordion it will get removed from the object.

[0,3] = Accordion 1 and 4 is open

[0,1] = Accordion 1 and 2 is open

4

1 回答 1

0

好的,所以我修补了我的插件,只从活动字符串中获取数字,但我也修改了我的对象,因为它被破坏了:

if (tab in active && active[tab] !== null) {
    delete active[tab];
} else {
    active[tab] = tab;
}

我得到 [null,1,null],所以现在我覆盖 null 并将其替换为值 [0,1,null] 并且插件仅读取 0 和 1。

所以这么快就回答了我自己的问题!

于 2013-05-04T07:09:56.687 回答