简体   繁体   English

SessionStorage保存DIV显示?

[英]SessionStorage to save DIV display?

Can I set the display style of my div using session storage? 我可以使用会话存储设置div的显示样式吗? I have a button and when the button is clicked it will display another div that is normally hidden. 我有一个按钮,当单击该按钮时,它将显示另一个通常隐藏的div。 I would like for it to set if it has been clicked and then remember the state after the page is refreshed? 我想设置它是否已被点击,然后记住刷新页面后的状态?

Here is my code: 这是我的代码:

<button id="ShopParts">Shop Parts</button>

$("#ShopParts").click(function() {
  $("#shop_vehicle_container").css("display", "block");
});

Yes. 是。

var el = $('#shop_vehicle_container');
var style = el.css('display');

// Store
localStorage.setItem("savedstyle", style);
// Retrieve & set
el.css('display', localStorage.getItem("savedstyle"));

Remember to check for Storage browser support. 请记住检查存储浏览器支持。

$(document).ready(function () {
    if (sessionStorage.getItem("shop-vehicle"))
    {
        $("#shop_vehicle_container").css("display", sessionStorage.getItem("shop-vehicle"));
    }
});
$("#ShopParts").click(function () {
    $("#shop_vehicle_container").css("display", "block");
    sessionStorage.setItem("shop-vehicle", $("#shop_vehicle_container").css("display"));
});

cookies work too.. 饼干也有效..

function setCookie(name, value) {
    var cookie = name + "=" + escape(value) + ";";
    document.cookie = cookie;
}

function getCookie(name) {
  var regexp = new RegExp("(?:^" + name + "|;\s*"+ name + ")=(.*?)(?:;|$)", "g");
  var result = regexp.exec(document.cookie);
  return (result === null) ? null : result[1];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM