恢复 css 属性 共 div
Restore css property of div
我正在尝试在我的站点刷新后从 div 恢复显示 属性(none
或 block
)。我有多个 div 和那个 class,但它们有一个唯一的 ID。在那些 div 中有一些复选框,它们在获得 checked/unchecked.
后刷新站点
这是一个 jsfiddle:https://jsfiddle.net/074jatfs/2/
这是我的侧边栏,带有 div 和复选框:
<div class="filter-panel filter--property facet--property" data-filter-type="value-list" data-field-name="f">
<div class="filter-panel--flyout" style="border:none;"><div id="label-it" class="label-it"><label class="filter-panel--title"><h3>Filter Group 1</h3></label></div>
<div class="filter-panel--content" id="FilterGroup1">
<ul class="filter-panel--option-list" style="list-style-type:none;">
<li class="filter-panel--option">
<div class="option--container" style="display:block;">
<span class="filter-panel--checkbox"><input type="checkbox" value="603"><span class="checkbox--state"> </span>
</span>
<label>Filter 1</label>
</div>
</li>
<li class="filter-panel--option">
<div class="option--container" style="display:block;">
<span class="filter-panel--checkbox"><input type="checkbox" value="613"><span class="checkbox--state"> </span>
</span>
<label>Filter 2</label>
</div>
</li>
</ul>
</div>
</div>
<div class="filter-panel filter--property facet--property" data-filter-type="value-list" data-field-name="f">
<div class="filter-panel--flyout" style="border:none;"><div id="label-it" class="label-it"><label class="filter-panel--title"><h3>Filter Group 2</h3></label></div>
<div class="filter-panel--content" id="FilterGroup2">
<ul class="filter-panel--option-list" style="list-style-type:none;">
<li class="filter-panel--option">
<div class="option--container" style="display:block;">
<span class="filter-panel--checkbox"><input type="checkbox" value="603"><span class="checkbox--state"> </span>
</span>
<label>Filter 3</label>
</div>
</li>
<li class="filter-panel--option">
<div class="option--container" style="display:block;">
<span class="filter-panel--checkbox"><input type="checkbox" value="613"><span class="checkbox--state"> </span>
</span>
<label>Filter 4</label>
</div>
</li>
</ul>
</div>
</div>
<div class="filter-panel filter--property facet--property" data-filter-type="value-list" data-field-name="f">
<div class="filter-panel--flyout" style="border:none;"><div id="label-it" class="label-it"><label class="filter-panel--title"><h3>Filter Group 3</h3></label></div>
<div class="filter-panel--content" id="FilterGroup3">
<ul class="filter-panel--option-list" style="list-style-type:none;">
<li class="filter-panel--option">
<div class="option--container" style="display:block;">
<span class="filter-panel--checkbox"><input type="checkbox" value="603"><span class="checkbox--state"> </span>
</span>
<label>Filter 5</label>
</div>
</li>
<li class="filter-panel--option">
<div class="option--container" style="display:block;">
<span class="filter-panel--checkbox"><input type="checkbox" value="613"><span class="checkbox--state"> </span>
</span>
<label>Filter 6</label>
</div>
</li>
</ul>
</div>
</div>
单击 div label-it
时,div filter-panel--content
从 display:block
切换到 display:none
,反之亦然(display:none
到 display:block
)
此时我想保存display
当前的属性,等网站刷新时恢复
这是我第一次尝试sessionStorage属性,我尝试在toggle函数中设置它:
$('.label-it').click(function() {
$(this).closest(".filter-panel--flyout").find(".filter-panel--content div").toggle(350)
sessionStorage.setItem("filterstate", $("#FilterGroup1").css("display"));
});
要恢复 filterstate
我试过这个:
$(document).ready(function () {
sessionStorage.getItem("filterstate");
if (sessionStorage.getItem("filterstate"))
{
$("#FilterGroup1").css("display", sessionStorage.getItem("filterstate"));
}
});
我无法恢复 css 属性,有人可以帮助我吗?请。
您需要改为尝试为 div 存储实际切换的 display
值
而不是 parent.
sessionStorage.setItem("filterstate", $("#FilterGroup1 .option--container").css("display"));
我正在尝试在我的站点刷新后从 div 恢复显示 属性(none
或 block
)。我有多个 div 和那个 class,但它们有一个唯一的 ID。在那些 div 中有一些复选框,它们在获得 checked/unchecked.
这是一个 jsfiddle:https://jsfiddle.net/074jatfs/2/
这是我的侧边栏,带有 div 和复选框:
<div class="filter-panel filter--property facet--property" data-filter-type="value-list" data-field-name="f">
<div class="filter-panel--flyout" style="border:none;"><div id="label-it" class="label-it"><label class="filter-panel--title"><h3>Filter Group 1</h3></label></div>
<div class="filter-panel--content" id="FilterGroup1">
<ul class="filter-panel--option-list" style="list-style-type:none;">
<li class="filter-panel--option">
<div class="option--container" style="display:block;">
<span class="filter-panel--checkbox"><input type="checkbox" value="603"><span class="checkbox--state"> </span>
</span>
<label>Filter 1</label>
</div>
</li>
<li class="filter-panel--option">
<div class="option--container" style="display:block;">
<span class="filter-panel--checkbox"><input type="checkbox" value="613"><span class="checkbox--state"> </span>
</span>
<label>Filter 2</label>
</div>
</li>
</ul>
</div>
</div>
<div class="filter-panel filter--property facet--property" data-filter-type="value-list" data-field-name="f">
<div class="filter-panel--flyout" style="border:none;"><div id="label-it" class="label-it"><label class="filter-panel--title"><h3>Filter Group 2</h3></label></div>
<div class="filter-panel--content" id="FilterGroup2">
<ul class="filter-panel--option-list" style="list-style-type:none;">
<li class="filter-panel--option">
<div class="option--container" style="display:block;">
<span class="filter-panel--checkbox"><input type="checkbox" value="603"><span class="checkbox--state"> </span>
</span>
<label>Filter 3</label>
</div>
</li>
<li class="filter-panel--option">
<div class="option--container" style="display:block;">
<span class="filter-panel--checkbox"><input type="checkbox" value="613"><span class="checkbox--state"> </span>
</span>
<label>Filter 4</label>
</div>
</li>
</ul>
</div>
</div>
<div class="filter-panel filter--property facet--property" data-filter-type="value-list" data-field-name="f">
<div class="filter-panel--flyout" style="border:none;"><div id="label-it" class="label-it"><label class="filter-panel--title"><h3>Filter Group 3</h3></label></div>
<div class="filter-panel--content" id="FilterGroup3">
<ul class="filter-panel--option-list" style="list-style-type:none;">
<li class="filter-panel--option">
<div class="option--container" style="display:block;">
<span class="filter-panel--checkbox"><input type="checkbox" value="603"><span class="checkbox--state"> </span>
</span>
<label>Filter 5</label>
</div>
</li>
<li class="filter-panel--option">
<div class="option--container" style="display:block;">
<span class="filter-panel--checkbox"><input type="checkbox" value="613"><span class="checkbox--state"> </span>
</span>
<label>Filter 6</label>
</div>
</li>
</ul>
</div>
</div>
单击 div label-it
时,div filter-panel--content
从 display:block
切换到 display:none
,反之亦然(display:none
到 display:block
)
此时我想保存display
当前的属性,等网站刷新时恢复
这是我第一次尝试sessionStorage属性,我尝试在toggle函数中设置它:
$('.label-it').click(function() {
$(this).closest(".filter-panel--flyout").find(".filter-panel--content div").toggle(350)
sessionStorage.setItem("filterstate", $("#FilterGroup1").css("display"));
});
要恢复 filterstate
我试过这个:
$(document).ready(function () {
sessionStorage.getItem("filterstate");
if (sessionStorage.getItem("filterstate"))
{
$("#FilterGroup1").css("display", sessionStorage.getItem("filterstate"));
}
});
我无法恢复 css 属性,有人可以帮助我吗?请。
您需要改为尝试为 div 存储实际切换的 display
值
而不是 parent.
sessionStorage.setItem("filterstate", $("#FilterGroup1 .option--container").css("display"));