简体   繁体   English

在页面之间维护Bootstrap的激活的导航标签

[英]Maintain Bootstrap's activated nav-tab between page

I am using Bootstrap 3. I currently have 2 pages, one is the view page and one is the edit page. 我正在使用Bootstrap3。我目前有2个页面,一个是查看页面,一个是编辑页面。 Both pages have a number of nav-tabs, say id= tab1, tab2, tab3. 两个页面都有许多导航标签,例如id = tab1,tab2,tab3。

What I am trying to achieve is, when I'm on tab2 of the view page, if I click the edit button, I will automatically go to tab2 of the edit page. 我想要实现的是,当我位于视图页面的tab2上时,如果单击“编辑”按钮,我将自动转到编辑页面的tab2。 And when I'm on tab3 of the edit page, if I press cancel, I will automatically go to tab3 of the view page. 当我进入编辑页面的tab3时,如果按“取消”,我将自动进入查看页面的tab3。

My current idea is to append tab id to the URL parameter, and on page ready, the page read reads the URL and activated the given tab. 我当前的想法是将选项卡ID附加到URL参数,在页面准备就绪时,页面读取将读取URL并激活给定的选项卡。 This is my code for activating tab. 这是我激活标签页的代码。

$(document).ready(function(){
    activaTab(getUrlVars()["tabId"]);
});

function getUrlVars() {
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

function activaTab(tab){
    $('.nav-tabs a[href="#' + tab + '"]').tab('show');
};

However, I am having troubles appending id to the URL on button click both 但是,我在将id附加到单击按钮的URL时遇到麻烦

<a class="btn">
and
 <button type="submit" class="btn"> <button type =“ submit” class =“ btn”> 

I'm just wondering if anyone have done similar thing? 我只是想知道是否有人做过类似的事情? Is there a better solution? 有更好的解决方案吗? With sample code please. 请提供示例代码。

I think you can use: 我认为您可以使用:

$('.btn').on('click',function(){
        window.location.href = 'edit.html' + window.location.hash; //the current hash
});

See also: 也可以看看:

Below is how I solved this 下面是我如何解决这个问题

I gave my ref link a class name called viewToEdit, then I stopped the default behaviour of the link (other wise it will ignore the following code). 我给我的ref链接一个名为viewToEdit的类名,然后我停止了该链接的默认行为(否则,它将忽略以下代码)。 And I extracted currently activated tab id using jQuery, and append that to my destination URL. 然后,我使用jQuery提取了当前激活的标签页ID,并将其附加到目标网址中。 Below is my code. 下面是我的代码。

$("a.viewToEdit").on('click', function(event) {
    event.preventDefault();
    var activatedDiv = $('div.tab-pane.form-tab-pane.active');
    if(typeof activatedDiv.attr("id") !== "undefined") {
        $(location).attr('href',$(this).attr("href")+"?tabId="+activatedDiv.attr("id"));
    } else {
        $(location).attr('href',$(this).attr("href"));
    }
});

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

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