LoginSignup
7

More than 5 years have passed since last update.

ハッシュタグの変更時にイベントを発生させる

Last updated at Posted at 2016-05-17

ハッシュタグ(#xxx)によってタブ切り替えをしておいたりするとき、(グローバルメニューとか)
違うページから遷移してきた時は問題なく動くが、同じページ内でアクセスしようとすると、ページ遷移しないので、うまく動かない。

hashchangeというものがある。

$(window).on('load hashchange',function(){
    var color;
    if(location.hash == '#01'){
        color = 'black';
    }else if(location.hash == '#02'){
        color = 'white';
    }
});

例えばこんな感じで使う。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7