0

我试图弄清楚它是否可能以及如何将使用 javascript 对页面所做的更改保存到浏览器。

例如:我有一个隐藏菜单的开关。所以我隐藏我的菜单并离开页面。当我再次访问此页面时,我希望该菜单保持隐藏状态。截至目前,一旦我离开会话或刷新,我使用 Javascript 所做的任何更改都会消失

4

1 回答 1

1

Not like you are thinking, no. There is no "remember this page state" flag or anything.

However, there is a way to store data. On modern browsers, you can use localStorage to persist some choice or data the user makes. Then when the page is loaded, your javascript can read that data and then configure the page around.

For example:

$('#hide-stuff-button').click(function() {
  $('#stuff').hide();
  localStorage.hidden = true;
});

if (localStorage.hidden) {
  $('#stuff').hide();
}

That script does 2 things. It saves some preference when you click a button, and when the page loads, it reads the saved state and does something to the page.

于 2013-03-15T02:09:49.403 回答