LoginSignup
0
0

More than 5 years have passed since last update.

グローバルナビなど(ul li a構成)に自動でクラス付与するjavascript(JQuery)

Posted at

動機

グローバルナビに自動で[active]クラスを付与したかった。

方法

jqeruy(javascrip)を記載する

コード

test.js

$('ul.classname li a').each(function(){
  if($($(this))[0].href==String(window.location)) {
    $(this).parent().addClass('active');
  }
});
//二階層等にも適用
$('ul.clssname li ul li a').each(function(){
  if($($(this))[0].href==String(window.location)) {
    $(this).parent().addClass('active');
    $(this).parent().parent().show();
  }
});

不具合あったら教えて下さい。

0
0
0

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
0
0