简体   繁体   中英

trigger jquery class change from A element on other page

I have a website I just took over maintaining. The previous Dev built a simple jquery script to hide/make visible li elements on a single webpage based on the.active class being applied. The issue I have is figuring out how to trigger the script to make visible selected hidden elements when coming from another page via hyperlink. Right now when going to the page the first li element is automatically loaded and the rest hidden.

var cooki = Cookies.get('Tab');
// var cooki=getCookie("Tab");
console.log(cooki);
//alert(cooki);
$(document).ready(function() {
  cooki;
  var id = '#' + cooki;
  if (id == "#Biofeedback") {
    $('html, body').animate({
      scrollTop: $("div").offset().top
    }, 10);
    $(id).addClass('active');
    $('.nacc li.active').removeClass('active');
    $(".naccs ul").find("li:eq(" + 0 + ")").addClass("active");
  } else if (id == "#hrv") {
    $('html, body').animate({
      scrollTop: $("div").offset().top
    }, 10);
    $(id).addClass('active');
    $('.nacc li.active').removeClass('active');
    $(".naccs ul").find("li:eq(" + 1 + ")").addClass("active");
  } else if (id == "#Neurofeedback") {
    $('html, body').animate({
      scrollTop: $("div").offset().top
    }, 10);
    $(id).addClass('active');
    $('.nacc li.active').removeClass('active');
    $(".naccs ul").find("li:eq(" + 2 + ")").addClass("active");
  } else if (id == "#muscle-rehabilitation") {
    //alert('success');
    $(id).addClass('active');
    $('.nacc li.active').removeClass('active');
    $(".naccs ul").find("li:eq(" + 3 + ")").addClass("active");
  } else if (id == "#peak-performance") {
    $('html, body').animate({
      scrollTop: $("div").offset().top
    }, 10);
    $(id).addClass('active');

    $('.nacc li.active').removeClass('active');
    $(".naccs ul").find("li:eq(" + 4 + ")").addClass("active");
  } else if (id == "#Continence-training") {
    $('html, body').animate({
      scrollTop: $("div").offset().top
    }, 10);
    $(id).addClass('active');
    $('.nacc li.active').removeClass('active');
    $(".naccs ul").find("li:eq(" + 5 + ")").addClass("active");
  } else if (id == "#Research") {
    $('html, body').animate({
      scrollTop: $("div").offset().top
    }, 10);
    //alert('success')
    $(id).addClass('active');
    $('.nacc li.active').removeClass('active');
    $(".naccs ul").find("li:eq(" + 6 + ")").addClass("active");

  }

});

From the remote page, you can setup the link to have /link#hash or /link#example

And on your current page you can do something like:

if(window.location.hash == '#example') {
  // do some JS/Jquery magic
}

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