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

jQuery Cookie使用指南:如何用非remembered命名的cookie匹配编辑页标签

解决方案

核心思路是提取Cookie中存储的tab序号,拼接当前页面的tab前缀匹配对应标签,修改后的完整代码如下:

$(document).ready(function() {
  // 先获取当前页面的tab前缀,自动适配信息页/编辑页
  const currentTabPrefix = $("a.tab").first().attr("tab-title").split("-")[0];
  const defaultTabId = `${currentTabPrefix}-tab1`;

  $("a.tab").click(function() {
    var activeTabId = $(this).attr("tab-title");
    $(".active").removeClass("active");
    $(this).addClass("active");

    $(".cont").slideUp();
    $("#" + activeTabId).slideDown();
    
    $.cookie('activeTab', activeTabId, {
      expires: 7
    });
  });
  
  var selectedID = $.cookie('activeTab');
  var targetTabId = defaultTabId;

  if (selectedID) {
    // 提取tab序号部分,比如nodeinfo-tab2提取出tab2
    const tabSuffix = selectedID.split("-")[1];
    targetTabId = `${currentTabPrefix}-${tabSuffix}`;
  }

  // 激活对应标签
  $(".active").removeClass("active");
  $(".cont").hide();
  $(`a.tab[tab-title="${targetTabId}"]`).addClass('active');
  $("#" + targetTabId).show();
});

代码说明

  • 自动识别当前页面的tab前缀:通过首个标签的tab-title属性提取前缀,无需手动区分信息页/编辑页,后续新增其他前缀的tab页面也可复用
  • 提取Cookie存储的tab序号,拼接当前页前缀生成对应标签ID,实现跨页标签匹配
  • 修复了原代码中默认tab值未加引号导致的变量未定义报错
  • 选择器添加了引号包裹属性值,避免特殊字符导致的选择器语法错误

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 15:24:04