You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何利用sessionStorage实现页面刷新后保留激活标签页

保留刷新前的激活标签页解决方案

你考虑的localStorage是完全可行的方案,核心思路就是切换标签时记录当前激活的标签ID,页面加载时优先读取记录并恢复。下面是修改后的完整代码:

修改后的JavaScript代码

function openCity(evt, cityName) {
  var i, tabcontent, tablinks;
  tabcontent = document.getElementsByClassName("tabcontent");
  for (i = 0; i < tabcontent.length; i++) {
    tabcontent[i].style.display = "none";
  }
  tablinks = document.getElementsByClassName("tablinks");
  for (i = 0; i < tablinks.length; i++) {
    tablinks[i].className = tablinks[i].className.replace(" active", "");
  }
  document.getElementById(cityName).style.display = "block";
  evt.currentTarget.className += " active";
  
  // 关键:将当前激活的标签页名称存入localStorage
  localStorage.setItem('activeTab', cityName);
}

// 页面加载时恢复标签页
window.addEventListener('load', function() {
  // 读取localStorage中保存的标签页,没有则用默认的London
  var savedTab = localStorage.getItem('activeTab') || 'London';
  // 找到对应标签按钮并触发点击
  document.querySelector(`.tablinks[onclick="openCity(event, '${savedTab}')"]`).click();
});

核心逻辑说明

  1. 存储激活状态:每次切换标签时,用localStorage.setItem()把当前标签的ID存入浏览器本地存储,键名设为activeTab,数据会一直保留直到主动清除。
  2. 页面加载恢复:页面加载完成后,读取localStorage中保存的标签ID,若存在则触发对应标签按钮的点击事件;若不存在(比如首次访问),则默认激活London标签。
  3. 表单适配:如果你的表单是传统刷新提交模式,无需额外修改——localStorage在页面刷新后依然有效,上述代码会自动读取并恢复到之前的标签页。

原CSS和HTML代码(仅补充表单示例)

CSS代码(无需修改)

/* Style the tab */
.tab {
  float: left;
  border: 1px solid #ccc;
  background-color: #f1f1f1;
  width: 30%;
  height: 300px;
}

/* Style the buttons inside the tab */
.tab button {
  display: block;
  background-color: inherit;
  color: black;
  padding: 22px 16px;
  width: 100%;
  border: none;
  outline: none;
  text-align: left;
  cursor: pointer;
  transition: 0.3s;
  font-size: 17px;
}

/* Change background color of buttons on hover */
.tab button:hover {
  background-color: #ddd;
}

/* Create an active/current "tab button" class */
.tab button.active {
  background-color: #ccc;
}

/* Style the tab content */
.tabcontent {
  float: left;
  padding: 0px 12px;
  border: 1px solid #ccc;
  width: 70%;
  border-left: none;
  height: 300px;
}

HTML代码(补充表单示例)

<div class="tab">
  <button class="tablinks" onclick="openCity(event, 'London')" id="defaultOpen">London</button>
  <button class="tablinks" onclick="openCity(event, 'Paris')">Paris</button>
  <button class="tablinks" onclick="openCity(event, 'Tokyo')">Tokyo</button>
</div>

<div id="London" class="tabcontent">
  <h3>London</h3>
  <p>London is the capital city of England.</p>
  <form action="#" method="get">
    <input type="text" placeholder="输入内容">
    <button type="submit">保存更改</button>
  </form>
</div>

<div id="Paris" class="tabcontent">
  <h3>Paris</h3>
  <p>Paris is the capital of France.</p>
  <form action="#" method="get">
    <input type="text" placeholder="输入内容">
    <button type="submit">保存更改</button>
  </form>
</div>

<div id="Tokyo" class="tabcontent">
  <h3>Tokyo</h3>
  <p>Tokyo is the capital of Japan.</p>
  <form action="#" method="get">
    <input type="text" placeholder="输入内容">
    <button type="submit">保存更改</button>
  </form>
</div>

内容的提问来源于stack exchange,提问作者Snorlax

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 03:10:36