简体   繁体   中英

JQuery; target and fire function only if in a certain directory

OK the results of my script aren't exactly producing the answer I am looking and the variable is coming back the opposite of what I am looking for:

//REVISED:This is in the mouseOut of .hover()

Here is my function:

$(".about,.activeAbout").hover(
  function () {
    $('#dropInvestments').css( "left","-9999px" );
    $('#dropMedia').css( "left","-9999px" );
    $('#dropAbout').css( "left","415px" );
    $('#aboutTitle').addClass("aboutTitleActive").removeClass("aboutTitle");
  }, 
      function () {
      $pageLoc = document.location.pathname.split('/about/');
      alert($pageLoc);
      if (document.location.pathname == $pageLoc) 

    {
      $('#aboutTitle').addClass("aboutTitle").removeClass("aboutTitleActive");
    }

}
);

Here is the pathname:

wga/x3/about/history.html

What i want is to only do the class change if the current page is not in the about folder/directory. Any thoughts?

Using split takes out the about and returns the pathname without it...

User regex .test to do this:

function () {
    var pattern = /^\/?wga\/x3\/about\/.*/;
    if (pattern.test(document.location.pathname)) {
        $('#aboutTitle').addClass("aboutTitle").removeClass("aboutTitleActive");
    }
}

Mad Thanks to Ohgodwhy and TheSmose for pointing me in the right direction, Here is how I got it to work. If you can make it simpler or have a better way feel free to post. Thank You:

$(".about,.activeAbout").hover(
  function () {
    $('#dropInvestments').css( "left","-9999px" );
    $('#dropMedia').css( "left","-9999px" );
    $('#dropAbout').css( "left","415px" );
    $('#aboutTitle').addClass("aboutTitleActive").removeClass("aboutTitle");
  }, 
  function () {
      $pageLoc = document.location.pathname;
      $tarLoc = /about/g;
      $result = $tarLoc.test($pageLoc);
      if ($result !== true) 

    {
      $('#aboutTitle').addClass("aboutTitle").removeClass("aboutTitleActive");
    }

  }
);

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