Javascript }); } } window.onload=函数(){invokeMeMaster();};

Javascript }); } } window.onload=函数(){invokeMeMaster();};,javascript,jquery,twitter-bootstrap,Javascript,Jquery,Twitter Bootstrap,这里是我制作的一个片段,它与bootstrap3和jQuery以及包含不同选项卡的不同URL一起工作虽然它不支持每页有多个选项卡,但如果您需要该功能,它应该是一个简单的修改 /** *处理“引导”包。 * *@namespace引导_ */ /** *@var{String} */ var bootstrap_uri_to_tab_key='bootstrap_uri_to_tab'; /** *@return{String} */ 函数bootstrap_get_uri() { 返回windo

这里是我制作的一个片段,它与bootstrap3jQuery以及包含不同选项卡的不同URL一起工作虽然它不支持每页有多个选项卡,但如果您需要该功能,它应该是一个简单的修改

/**
*处理“引导”包。
*
*@namespace引导_
*/
/**
*@var{String}
*/
var bootstrap_uri_to_tab_key='bootstrap_uri_to_tab';
/**
*@return{String}
*/
函数bootstrap_get_uri()
{
返回window.location.href;
}
/**
*@return{Object}
*/
函数引导\加载\选项卡\数据()
{
var uriToTab=localStorage.getItem(引导\u uri\u到\u选项卡\u键);
if(尿毒抗体){
试一试{
uriToTab=JSON.parse(uriToTab);
如果(uriToTab的类型!=“对象”){
uriToTab={};
}
}捕捉(错误){
uriToTab={};
}
}否则{
uriToTab={};
}
返回uriToTab;
}
/**
*@param{Object}数据
*/
功能引导\保存\选项卡\数据(数据)
{
setItem(bootstrap_uri_to_tab_key,JSON.stringify(data));
}
/**
*@param{String}href
*/
功能引导\保存\选项卡(href)
{
var uri=bootstrap_get_uri();
var uriToTab=bootstrap_load_tab_data();
uriToTab[uri]=href;
引导存储选项卡数据(uriToTab);
}
/**
*
*/
函数引导\恢复\选项卡()
{
var uri=bootstrap_get_uri();
var uriToTab=bootstrap_load_tab_data();
if(uriToTab.hasOwnProperty(uri)&&
$('[href=“'+uriToTab[uri]+'“]')。长度){
}否则{
uriToTab[uri]=$('a[data toggle=“tab”]:first').attr('href');
}
if(uriToTab[uri]){
$('[href=“'+uriToTab[uri]+'“]')。选项卡('show');
}
}
$(文档).ready(函数(){
如果($('.nav制表符')。长度){
//对于引导程序3,使用'show.bs.tab',对于引导程序2,使用下一行中的'show'
$('a[data toggle=“tab”]”)on('show.bs.tab',函数(e){
bootstrap_save_选项卡($(this.attr('href'));
});
引导恢复选项卡();
}
});
$(文档).ready(函数(){


}))

如果页面中有多个选项卡,可以使用以下代码

<script type="text/javascript">
$(document).ready(function(){
    $('#profileTabs').on('show.bs.tab', function(e) {
        localStorage.setItem('profileactiveTab', $(e.target).attr('href'));
    });
    var profileactiveTab = localStorage.getItem('profileactiveTab');
    if(profileactiveTab){
        $('#profileTabs a[href="' + profileactiveTab + '"]').tab('show');        
    }
    $('#charts-tab').on('show.bs.tab', function(e) {
        localStorage.setItem('chartsactiveTab', $(e.target).attr('href'));
    });
    var chartsactiveTab = localStorage.getItem('chartsactiveTab');
    if(chartsactiveTab){
        $('#charts-tab a[href="' + chartsactiveTab + '"]').tab('show');        
    }     
});
</script>

$(文档).ready(函数(){
$('#profileTabs').on('show.bs.tab',函数(e){
setItem('profileactiveTab',$(e.target).attr('href');
});
var profileactiveTab=localStorage.getItem('profileactiveTab');
如果(profileactiveTab){
$(“#profileTabs a[href=“”+profileactiveTab+“]”)。tab('show');
}
$(“#图表选项卡”).on('show.bs.tab',函数(e){
localStorage.setItem('chartsactiveTab',$(e.target.attr('href'));
});
var chartsativetab=localStorage.getItem('chartsativetab');
如果(chartsactiveTab){
$(“#图表选项卡a[href=“”+图表活动选项卡+”).tab('show');
}     
});

这将刷新选项卡,但仅在加载控制器中的所有内容之后

// >= angular 1.6 angular.element(function () {
angular.element(document).ready(function () {
    //Here your view content is fully loaded !!
    $('li[href="' + location.hash + '"] a').tab('show');
});

我将其用于MVC:

  • 模型中有一个SelectedTab整数字段,用于将值发送到POST方法
JavaScript部分:

<script type="text/javascript">
    $(document).ready(function () {
       var index = $("input#SelectedTab").val();
       $("#tabstrip > ul li:eq(" + index + ")").addClass("k-state-active");

       $("#tabstrip").kendoTabStrip();
    });
    function setTab(index) {
      $("input#SelectedTab").val(index)
    }
</script>

$(文档).ready(函数(){
var索引=$(“输入#选择的选项卡”).val();
$(“#tabstrip>ulli:eq(“+index+”)).addClass(“k-state-active”);
$(“#tabstrip”).kendotastrip();
});
函数设置选项卡(索引){
$(“input#SelectedTab”).val(索引)
}
HTML部分:

@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.HiddenFor(model => model.SelectedTab)
<div id="tabstrip">
    <ul>
        <li onclick="setTab(0)">Content 0</li>
        <li onclick="setTab(1)">Content 1</li>
        <li onclick="setTab(2)">Content 2</li>
        <li onclick="setTab(3)">Content 3</li>
        <li onclick="setTab(4)">Content 4</li>
    </ul>
    <div>

    </div>
    <div>

    </div>
    <div>

    </div>
    <div>

    </div>
    <div>

    </div>
</div>
<div class="content">
    <button type="submit" name="save" class="btn bg-blue">Save</button>
</div>
}
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.HiddenFor(model=>model.SelectedTab)
  • 内容0
  • 内容1
  • 内容2
  • 内容3
  • 内容4
拯救 }
nice one-bud,您的解决方案可能会添加到joomla 30 core中,最好使用
$(this.attr('href')
,而不是
$(e.target).attr('id')
,它提供了没有完整url的哈希。您还必须使用
data toggle=“tab”
在a标记上应用
.tab()
,而不是
$('#'+lastTab)
。另请参见:在Bootstrap3中,该方法似乎略有更改<代码>显示似乎不再工作,必须将其更改为
显示.bs.tab
。注:我了解javascript等,也了解经济学。。。因此,如果有人知道发生了什么,请随时纠正我。修改为使用多个选项卡控件(和Bootstrap 3):它将转到第一个选项卡,刷新后返回到当前选项卡。我无法使localStorage解决方案正常工作,尽管它看起来是合乎逻辑的。这个开箱即用的解决方案和$.cookie插件非常方便,在带有data toggle=“tab”的a标记上添加.apply.tab(),而不是删除和添加类。另请参见:对于我的布局,因为我对div使用了“淡入活动”,所以需要更改最后两行以添加或删除“处于活动状态”。谢谢您的回答。它对我来说很好,我不想仅仅为了这个问题而处理cookies,所以它很好。@koppor,这不起作用,因为有回发事件。我添加了linkbutton
Search
,单击按钮后,默认选项卡变为活动状态,而不是当前选项卡。如何解决这个问题?这似乎是一个重复的问题,对于BS3,将“show”事件替换为“show.bs.tab”我认为您的代码在显示getItem之前的方式是错误的,这也解决了两次声明indexTab的问题。@JohnMagnolia我将
indexTab
命名了两次,但这是一个差异索引。因此,我将调用第二个
lastIndexTab
。对于显示的
,这是一个事件,因此在打开选项卡之前它不会触发,所以在
getItem
之前也不重要。
<ul class="nav nav-tabs">
<div id="p1" class="tab-pane">
    $(function() { 
        $('a[data-toggle="tab"]').on('shown', function(e){
            //save the latest tab using a cookie:
            $.cookie('last_tab', $(e.target).attr('href'));
        });
        //activate latest tab, if it exists:
        var lastTab = $.cookie('last_tab');
        if (lastTab) {
            $('a[href=' + lastTab + ']').tab('show');
        }
        else
        {
            // Set the first tab if cookie do not exist
            $('a[data-toggle="tab"]:first').tab('show');
        }
    });
$(function() {
  var json, tabsState;
  $('a[data-toggle="pill"], a[data-toggle="tab"]').on('shown', function(e) {
    var href, json, parentId, tabsState;

    tabsState = localStorage.getItem("tabs-state");
    json = JSON.parse(tabsState || "{}");
    parentId = $(e.target).parents("ul.nav.nav-pills, ul.nav.nav-tabs").attr("id");
    href = $(e.target).attr('href');
    json[parentId] = href;

    return localStorage.setItem("tabs-state", JSON.stringify(json));
  });

  tabsState = localStorage.getItem("tabs-state");
  json = JSON.parse(tabsState || "{}");

  $.each(json, function(containerId, href) {
    return $("#" + containerId + " a[href=" + href + "]").tab('show');
  });

  $("ul.nav.nav-pills, ul.nav.nav-tabs").each(function() {
    var $this = $(this);
    if (!json[$this.attr("id")]) {
      return $this.find("a[data-toggle=tab]:first, a[data-toggle=pill]:first").tab("show");
    }
  });
});
$(function() { 
  //for bootstrap 3 use 'shown.bs.tab' instead of 'shown' in the next line
  $('a[data-toggle="tab"]').on('click', function (e) {
    //save the latest tab; use cookies if you like 'em better:
    localStorage.setItem('lastTab', $(e.target).attr('href'));
  });

  //go to the latest tab, if it exists:
  var lastTab = localStorage.getItem('lastTab');

  if (lastTab) {
    $('a[href="'+lastTab+'"]').click();
  }
});
// See http://stackoverflow.com/a/16984739/64904
// Updated by Larry to setup for fading
$(function() {
  var json, tabsState;
  $('a[data-toggle="pill"], a[data-toggle="tab"]').on('shown.bs.tab', function(e) {
    var href, json, parentId, tabsState;
    tabsState = localStorage.getItem("tabs-state");
    json = JSON.parse(tabsState || "{}");
    parentId = $(e.target).parents("ul.nav.nav-pills, ul.nav.nav-tabs").attr("id");
    href = $(e.target).attr('href');
    json[parentId] = href;
    return localStorage.setItem("tabs-state", JSON.stringify(json));
  });
  tabsState = localStorage.getItem("tabs-state");
  json = JSON.parse(tabsState || "{}");
  $.each(json, function(containerId, href) {
    var a_el = $("#" + containerId + " a[href=" + href + "]");
    $(a_el).parent().addClass("active");
    $(href).addClass("active in");
    return $(a_el).tab('show');
  });
  $("ul.nav.nav-pills, ul.nav.nav-tabs").each(function() {
    var $this = $(this);
    if (!json[$this.attr("id")]) {
      var a_el = $this.find("a[data-toggle=tab]:first, a[data-toggle=pill]:first"),
          href = $(a_el).attr('href');
      $(a_el).parent().addClass("active");
      $(href).addClass("active in");
      return $(a_el).tab("show");
    }
  });
});
<ul class="nav nav-tabs" id="myTab">
    <li class="active"><a href="#home">Home</a></li>
    <li><a href="#profile">Profile</a></li>
    <li><a href="#messages">Messages</a></li>
    <li><a href="#settings">Settings</a></li>
</ul>

<div class="tab-content">
    <div class="tab-pane active" id="home">home</div>
    <div class="tab-pane" id="profile">profile</div>
    <div class="tab-pane" id="messages">messages</div>
    <div class="tab-pane" id="settings">settings</div>
</div>

<script>
    $('#myTab a').click(function (e) {
        e.preventDefault()
        $(this).tab('show')
    });

    // store the currently selected tab in the hash value
    $("ul.nav-tabs > li > a").on("shown.bs.tab", function (e) {
        var id = $(e.target).attr("href").substr(1);
        window.location.hash = id;
    });

    // on load of the page: switch to the currently selected tab
    var hash = window.location.hash;
    $('#myTab a[href="' + hash + '"]').tab('show');
</script>
$(function() { 
  $('#div a[data-toggle="tab"]').on('shown', function (e) {
    amplify.store(window.location.hostname+'last_used_tab', $(this).attr('href'));
  });

  var lastTab = amplify.store(window.location.hostname+'last_used_tab');
  if (lastTab) {
    $("#div a[href="+ lastTab +"]").tab('show');
  }
});
$(document).ready(function(){
    //console.log($('a[data-toggle="tab"]:first').tab('show'))
    $('a[data-toggle="tab"]').on('shown.bs.tab', function () {
        //save the latest tab; use cookies if you like 'em better:
        localStorage.setItem('lastTab', $(this).attr('href'));
    });

    //go to the latest tab, if it exists:
    var lastTab = localStorage.getItem('lastTab');
    if ($('a[href=' + lastTab + ']').length > 0) {
        $('a[href=' + lastTab + ']').tab('show');
    }
    else
    {
        // Set the first tab if cookie do not exist
        $('a[data-toggle="tab"]:first').tab('show');
    }
})
$(document).ready(function(){
    //console.log($('a[data-toggle="tab"]:first').tab('show'))
    $('a[data-toggle="tab"]').on('shown.bs.tab', function () {
        //save the latest tab; use cookies if you like 'em better:
        localStorage.setItem('lastTab_klanten', $(this).attr('href'));
    });

    //go to the latest tab, if it exists:
    var lastTab_klanten = localStorage.getItem('lastTab_klanten');
    if (lastTab_klanten) {
        $('a[href=' + lastTab_klanten + ']').tab('show');
    }
    else
    {
        // Set the first tab if cookie do not exist
        $('a[data-toggle="tab"]:first').tab('show');
    }
})
$(function() { 
  $('a[data-toggle="tab"]').on('shown', function (e) {
    var indexTab = $('a[data-toggle="tab"]').index($(this)); // this: current tab anchor
    localStorage.setItem('lastVisitedTabIndex', indexTab);
  });

  //go to the latest tab, if it exists:
  var lastIndexTab  = localStorage.getItem('lastVisitedTabIndex');
  if (lastIndexTab) {
      $('a[data-toggle="tab"]:eq(' + lastIndexTab + ')').tab('show');
  }
});
    private void ActiveTab(HtmlGenericControl activeContent, HtmlGenericControl activeTabStrip)
    {
        if (activeContent != null && activeTabStrip != null)
        {
            // Remove active from content
            Content1.Attributes["class"] = Content1.Attributes["class"].Replace("active", "");
            Content2.Attributes["class"] = Content2.Attributes["class"].Replace("active", "");
            Content3.Attributes["class"] = Content3.Attributes["class"].Replace("active", "");

            // Remove active from tab strip
            tabStrip1.Attributes["class"] = tabStrip1.Attributes["class"].Replace("active", "");
            tabStrip2.Attributes["class"] = tabStrip2.Attributes["class"].Replace("active", "");
            tabStrip3.Attributes["class"] = tabStrip3.Attributes["class"].Replace("active", "");

            // Set only active
            activeContent.Attributes["class"] = activeContent.Attributes["class"] + " active";
            activeTabStrip.Attributes["class"] = activeTabStrip.Attributes["class"] + " active";
        }
    }
$(".nav-tabs a").on("click", function() {
    location.hash = $(this).attr("href");
});
        if (JSON.parse(localStorage.getItem('currentClass')) == "active")
        {
            jQuery('#supporttbl').addClass('active')
            $('.sub-menu').css({ "display": "block" });
        }

        $("#supporttbl").click(function () { 
            var currentClass;
            if ($(this).attr('class')== "active") { 

                currentClass = $(this).attr('class');

                localStorage.setItem('currentClass', JSON.stringify(currentClass));
                console.log(JSON.parse(localStorage.getItem('currentClass')));

                jQuery('#supporttbl').addClass('active')
                $('.sub-menu').css({ "display": "block" });

            } else {

                currentClass = "Null"; 

                localStorage.setItem('currentClass', JSON.stringify(currentClass));
                console.log(JSON.parse(localStorage.getItem('currentClass')));

                jQuery('#supporttbl').removeClass('active')
                $('.sub-menu').css({ "display": "none" });

            }  
        });
<script type="text/javascript">
$(document).ready(function(){
    $('#profileTabs').on('show.bs.tab', function(e) {
        localStorage.setItem('profileactiveTab', $(e.target).attr('href'));
    });
    var profileactiveTab = localStorage.getItem('profileactiveTab');
    if(profileactiveTab){
        $('#profileTabs a[href="' + profileactiveTab + '"]').tab('show');        
    }
    $('#charts-tab').on('show.bs.tab', function(e) {
        localStorage.setItem('chartsactiveTab', $(e.target).attr('href'));
    });
    var chartsactiveTab = localStorage.getItem('chartsactiveTab');
    if(chartsactiveTab){
        $('#charts-tab a[href="' + chartsactiveTab + '"]').tab('show');        
    }     
});
</script>
// >= angular 1.6 angular.element(function () {
angular.element(document).ready(function () {
    //Here your view content is fully loaded !!
    $('li[href="' + location.hash + '"] a').tab('show');
});
<script type="text/javascript">
    $(document).ready(function () {
       var index = $("input#SelectedTab").val();
       $("#tabstrip > ul li:eq(" + index + ")").addClass("k-state-active");

       $("#tabstrip").kendoTabStrip();
    });
    function setTab(index) {
      $("input#SelectedTab").val(index)
    }
</script>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.HiddenFor(model => model.SelectedTab)
<div id="tabstrip">
    <ul>
        <li onclick="setTab(0)">Content 0</li>
        <li onclick="setTab(1)">Content 1</li>
        <li onclick="setTab(2)">Content 2</li>
        <li onclick="setTab(3)">Content 3</li>
        <li onclick="setTab(4)">Content 4</li>
    </ul>
    <div>

    </div>
    <div>

    </div>
    <div>

    </div>
    <div>

    </div>
    <div>

    </div>
</div>
<div class="content">
    <button type="submit" name="save" class="btn bg-blue">Save</button>
</div>
}