简体   繁体   English

Jquery选项卡保持选项卡打开,这是url中的subid

[英]Jquery tabs keep tab open that is subid in url

i am using jquery tabs, and trying to get pagination in each one, everything works fine however, if i click to go to the next page on say the second tab, it does it fine, but transports me to the first tab open, so you have to keep clicking to the second tab to view the new content. 我正在使用jquery选项卡,并试图在每一个中分页,一切正常但是,如果我点击进入下一页说第二个选项卡,它做得很好,但是把我带到第一个选项卡打开,所以您必须继续单击第二个选项卡才能查看新内容。 My question is how can i make it so that when the user clicks the next page in the pagination, the content is refreshed but the same tab is left open. 我的问题是如何使它成为当用户点击分页中的下一页时,内容被刷新但相同的选项卡保持打开状态。

My plan on how to incorporate this into my current code is to use the url mail.php?page=2&tid=2 我关于如何将其合并到我当前代码中的计划是使用url mail.php?page=2&tid=2

Where the page=2 is reference for the pagination that works fine but i want the tid (tab id) to make it so that that tab is the open one. 页面= 2是分页的参考,其工作正常,但我希望tid(选项卡ID)使其成为打开的选项卡。

Here is the javascript for the tabs you might recognise it 这是您可能认识到的标签的javascript

$(document).ready(function() {

    //Default Action
    $(".tab_content").hide(); //Hide all content
    $("ul.tabs li:first").addClass("active").show(); //Activate first tab
    $(".tab_content:first").show(); //Show first tab content

    //On Click Event
    $("ul.tabs li").click(function() {
        $("ul.tabs li").removeClass("active"); //Remove any "active" class
        $(this).addClass("active"); //Add "active" class to selected tab
        $(".tab_content").hide(); //Hide all tab content
        var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
        $(activeTab).fadeIn(); //Fade in the active content
        return false;
    });

});

And the html for the tabs (had to comment out for the lists) 标签的html(必须注释掉列表)

//<div id="tabsX">   <div id="tabContent">   <ul class="tabs">
   // <!--<li><a id="all" href="#all" class="all">All</a></li>-->
   // <li><a href="#inbox" class="inbox"></a></li>
   // <li><a href="#outbox" class="outbox"></a></li>
   // <li><a href="#compose" class="compose"></a></li>
    //<li><a href="#trash" class="trash"></a></li>
   // 

   // </ul>

AND the html for the tab content (only showing one as an example with no content as there will be too much to show) 和标签内容的html(仅显示一个没有内容的示例,因为会显示太多内容)

<div id="inbox" class="tab_content">
      <div id="inbox_header" style="display:table-cell; vertical-align:middle">
      <!---content--->          

      </div><!---end inbox_content--->



</div><!---end inbox--->

I would really appreciate any help as i can't seem to find any solutions for myself 我真的很感激任何帮助,因为我似乎无法为自己找到任何解决方案

You can use the following function from here to catch the tid param in javascript 您可以使用此处的以下函数来捕获javascript中的tid参数

function getParameterByName(name)
{
  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  var regexS = "[\\?&]" + name + "=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.search);
  if(results == null)
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}

Then change your code like this 然后像这样更改你的代码

var tabIndex = parseInt(getParameterByName('tid'),10);
$("ul.tabs li").eq(tabIndex - 1).addClass("active").show(); //Activate first tab
$(".tab_content").eq(tabIndex - 1).show(); //Show first tab content 

So you full js code would be 所以你完整的js代码将是

function getParameterByName(name)
    {
      name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
      var regexS = "[\\?&]" + name + "=([^&#]*)";
      var regex = new RegExp(regexS);
      var results = regex.exec(window.location.search);
      if(results == null)
        return "";
      else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
    } 
$(document).ready(function() {

    //Default Action
    $(".tab_content").hide(); //Hide all content

    var tabIndex = parseInt(getParameterByName('tid'),10);
    if(!tabIndex)
       tabIndex = 1;
    $("ul.tabs li").eq(tabIndex - 1).addClass("active").show(); //Activate first tab
    $(".tab_content").eq(tabIndex - 1).show(); //Show first tab content

    //On Click Event
    //  add you onlick code here


  )};

I am adding -1 in eq because it's 0-based . 我在eq添加-1 ,因为它是0-based Check the doc here $.eq 检查这里的文档$ .eq

I would suggest looking at the documentation for jquery tabs. 我建议查看jquery选项卡的文档。 There's a number of options that you can use. 您可以使用许多选项。 The cookie option: cookie选项:

$( ".selector" ).tabs({ cookie: { expires: 30 } });

Store the latest selected tab in a cookie. 将最新选定的选项卡存储在cookie中。 The cookie is then used to determine the initially selected tab if the selected option is not defined. 如果未定义所选选项,则cookie用于确定最初选择的选项卡。 Requires cookie plugin, which can also be found in the development-bundle>external folder from the download builder. 需要cookie插件,也可以在下载构建器的development-bundle> external文件夹中找到。 The object needs to have key/value pairs of the form the cookie plugin expects as options. 该对象需要具有cookie插件期望的形式的键/值对作为选项。 Available options (example): { expires: 7, path: '/', domain: 'jquery.com', secure: true }. 可用选项(示例):{expires:7,path:'/',domain:'jquery.com',secure:true}。 Since jQuery UI 1.7 it is also possible to define the cookie name being used via name property. 从jQuery UI 1.7开始,还可以通过name属性定义正在使用的cookie名称。

Also, you can use the selected option. 此外,您可以使用所选的选项。 This lets you specify what tab should be opened on initialization: 这允许您指定在初始化时应打开的选项卡:

$( ".selector" ).tabs({ selected: 3 });

The third option is to load the content of the tab via an ajax call instead. 第三种选择是通过ajax调用来加载选项卡的内容。

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

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