本地存储没有存储多个 slideToggle 的状态
Local-storage didn't store the state of multiple slideToggle
我想用 slideToggle 做一个多垂直菜单,并将切换状态存储在本地存储中,我不明白为什么不存储状态。
我发现这个例子 (https://codepen.io/johnyrodni/pen/VVMXJx) who works, but when I try to past it to my real needs (https://codepen.io/azyme/pen/PrGNpW) 没有本地存储了。
<div id="dropdown-1">
<div class="heading">
Click me
</div>
<ul class="content">
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
</ul>
</div>
<div id="dropdown-2">
<div class="heading">
Click me
</div>
<ul class="content">
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
</ul>
</div>
$(document).ready(function() {
var openTabs = [];
$(".content").hide();
$(".heading").click(function(){
var $this = $(this),
selector = $this.parent().attr('id') + ' .heading';
if ($(this).next('.content').is(':visible'))
{
var pos = openTabs.indexOf(selector);
openTabs.splice(pos, 1);
}
else
{
openTabs.push(selector);
}
localStorage.openTabs = openTabs.join(',');
$this.next(".content").slideToggle();
});
if (localStorage.openTabs)
$(localStorage.openTabs).click();
});
我希望切换的状态将存储在本地存储中
这应该可以解决问题。
替换此行:
localStorage.openTabs = openTabs.join(',');
有了这个:
localStorage.setItem('openTabs', JSON.stringify(openTabs.join(',')))
还有这个:
if (localStorage.openTabs)
$(localStorage.openTabs).click();
有了这个:
if (localStorage.hasOwnProperty('openTabs'))
$(JSON.parse(localStorage.getItem('openTabs'))).click();
我想用 slideToggle 做一个多垂直菜单,并将切换状态存储在本地存储中,我不明白为什么不存储状态。
我发现这个例子 (https://codepen.io/johnyrodni/pen/VVMXJx) who works, but when I try to past it to my real needs (https://codepen.io/azyme/pen/PrGNpW) 没有本地存储了。
<div id="dropdown-1">
<div class="heading">
Click me
</div>
<ul class="content">
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
</ul>
</div>
<div id="dropdown-2">
<div class="heading">
Click me
</div>
<ul class="content">
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
</ul>
</div>
$(document).ready(function() {
var openTabs = [];
$(".content").hide();
$(".heading").click(function(){
var $this = $(this),
selector = $this.parent().attr('id') + ' .heading';
if ($(this).next('.content').is(':visible'))
{
var pos = openTabs.indexOf(selector);
openTabs.splice(pos, 1);
}
else
{
openTabs.push(selector);
}
localStorage.openTabs = openTabs.join(',');
$this.next(".content").slideToggle();
});
if (localStorage.openTabs)
$(localStorage.openTabs).click();
});
我希望切换的状态将存储在本地存储中
这应该可以解决问题。
替换此行:
localStorage.openTabs = openTabs.join(',');
有了这个:
localStorage.setItem('openTabs', JSON.stringify(openTabs.join(',')))
还有这个:
if (localStorage.openTabs)
$(localStorage.openTabs).click();
有了这个:
if (localStorage.hasOwnProperty('openTabs'))
$(JSON.parse(localStorage.getItem('openTabs'))).click();