window.location.href上的jQuery执行此代码

时间:2015-10-09 10:34:30

标签: javascript jquery

我需要从home.html转到services.html,类.services_profile是一个子菜单,需要转到services.html并激活.services_profile_tab

$(".services_profile").click(function(e){

    $('.nav-tabs li a.tab-profile').attr("aria-expanded","true").parent().siblings().removeClass('active');
    $('.nav-tabs li a.tab-profile').parent().addClass('active');
    $('.tab-content').children().removeClass('in active');
    $('.tab-content .profile').addClass('in active');

    setTimeout(function() {
      window.location.href = 'services.html';
    },1000);

    // This does not work
    if (window.location.pathName.indexOf('services') >= 0) {
      $('services_profile_tab').addclass('active');
    }

});

2 个答案:

答案 0 :(得分:1)

pathname 应为小写:

if (window.location.pathName.indexOf('services') >= 0) {

而且你在.之前错过了一个services_profile_tab课程:

$('services_profile_tab').addclass('active');

希望这有帮助。

答案 1 :(得分:0)

您需要将标签引用传递到服务页面url或者您可以使用window.localStorage设置特定标签,以便在导航到特定页面时激活使用hash传递URL中的ref:

$(".services_profile").click(function(e){

    $('.nav-tabs li a.tab-profile').attr("aria-expanded","true")
                                   .parent().siblings().removeClass('active');
    $('.nav-tabs li a.tab-profile').parent().addClass('active');
    $('.tab-content').children().removeClass('in active');
    $('.tab-content .profile').addClass('in active');

    setTimeout(function() {
      window.location.href = 'services.html#services_profile_tab';// pass the tab reference
    },1000);

});

现在位于doc ready块的服务页面上:

$(function(){
  if (window.location.indexOf('services') >= 0) {
      var tab = window.location.hash.substr(1);
      $('.'+tab).addclass('active');
   }
});

您可以动态传递标签引用,例如:

window.location.href = this.className.split('_')[0]'.html#'+this.className+'_tab';
// so it results in
// services.html#services_profile_tab => when "services_profile" clicked

此处this.className是要点击的已点击元素。