LoginSignup
1
0

More than 5 years have passed since last update.

현재 페이지에 class 추가하기

Last updated at Posted at 2018-08-08

개념

var str = 'Mozilla';

console.log(str.substring(1, 3));
// expected output: "oz"

console.log(str.substring(2));
// expected output: "zilla"

현재 페이지에 클래스 추가하기

javascript, jquery を勉強したけどいつも難しい。:frowning2:

    // http://127.0.0.1/06_menu/feature/index.html //"/"[2]기준으로 feture 를 가져온다.
    var activeUrl = location.pathname.split("/")[2];

    // 문자열의 앞 20번째 인덱스까지 자른후 다음 문자열 추출, 20이후의 문자열 추출
    var activeSub = location.href.substring(20);

      navList = $(".menu-list > li").find("> a");
      subMenu = $('.sub-menu > li').find(">a");

      navList.each(function(){
        if( $(this).attr("href").split("/")[2] == activeUrl ) {
          console.log($(this).attr("href").split("/")[2],activeUrl);
         $(this).addClass("active");
        };
      });

      subMenu.each(function() {
        if ($(this).attr("href") == activeSub) {
          $(this).addClass("active");
        }
      });
    }
1
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
1
0