简体   繁体   中英

jQuery on window.location.href execute this code

I need to go from home.html to services.html, the class .services_profile is a sub-menu that needs to go to services.html and make the .services_profile_tab active

$(".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');
    }

});

The pathname should be in lowercase in :

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

And also you've missing a dot . of class before services_profile_tab in :

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

Hope this helps.

you need to pass the tab reference to the services page Either within the url or you can make use of window.localStorage to set the specific tab to get active on navigating to the specific page like this with passing a ref in the url with hash:

$(".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);

});

now on the services page in the doc ready block:

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

Something you can do to pass the tab reference dynamically like:

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

here this.className is the clicked element to navigate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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