1

我正在使用带有标签内容的 Jquery。无论我在哪个选项卡上,每当我刷新页面时,它总是返回到选项卡编号 1。如何让它留在我最初所在的选项卡上?

这是我的测试站点

谢谢你的帮助。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <title>test</title>
    <style type="text/css">
      #tabbed_box_1 {
        width: 855px;
      }
      .tabbed_area {
        background-color: #fff;
        padding: 0px;
      }
      ul.tabs {
        margin: 0px;
        padding: 0px;
      }
      ul.tabs li {
        display: inline;
        list-style: none;
      }
      ul.tabs li a {
        background-color: red;
        border: 1px solid #111;
        color: #fff;
        font-size: 20px;
        padding: 6px 16px;
      }
      ul.tabs li a:hover {
        border-color: #111;
        cursor: pointer;
      }
      ul.tabs li a.active {
        background-color: #fff;
        border: 1px solid #111;
        color: #666;
        cursor: pointer;
      }
      .tabrightcontent {
        background-color: #fff;
        border: 1px solid #111;
        height: 500px;
        padding: 20px;
      }
      #content_2,
      #content_3,
      #content_4,
      #content_5 {
        display: none;
      }
      ul.tabs {
        margin: 0px;
        margin-bottom: 0px;
        margin-top: 0px;
        padding: 0px;
      }
    </style>
  </head>
  <body>
    <script>
      $(document).ready(function () {
        $(".tabs a").click(function () {
          $(this).addClass("active").parent().siblings().find("a.active").removeClass("active");

          var id = $(this).parent().parent().find("a").index(this);
          $(this).parent().parent().parent().find(".tabrightcontent").css({
            display: "none"
          }).eq(id).css({
            display: "block"
          });
        });
      });
    </script>
    <div id="tabbed_box_1" class="tabbed_box">
      <div class="tabbed_area">
        <ul class="tabs">
          <li><a class="tab active">Tab Number 1</a></li>
          <li><a class="tab">Tab Number 2</a></li>
          <li><a class="tab">Tab Number 3</a></li>
        </ul>
        <div id="content_1" class="tabrightcontent">
          Tab Number 1 Content
        </div>
        <div id="content_2" class="tabrightcontent">
          Tab Number 2 Content
        </div>
        <div id="content_3" class="tabrightcontent">
          Tab Number 3 Content
        </div>
      </div>
    </div>
  </body>
</html>
4

2 回答 2

1

创建选项卡时使用该cookie选项。

有关详细信息,请参阅jQuery UI文档,但这里是一个示例:

// Initialize a tabs with the cookie option specified.
$( ".selector" ).tabs({ cookie: { expires: 30 } });
// Get or set the cookie option, after init.
//getter
var cookie = $( ".selector" ).tabs( "option", "cookie" );
//setter
$( ".selector" ).tabs( "option", "cookie", { expires: 30 } );
于 2012-04-24T20:05:09.670 回答
0

使用 document.location.hash,您可以在不刷新的情况下更改地址栏,因此每次单击选项卡时,将 document.location.hash 更改为唯一的(例如选项卡 ID)。

然后在页面加载时,检查 document.location.hash,如果它是选项卡的 id,则切换到该选项卡。

于 2012-04-24T20:00:51.537 回答