이전 목록 다음
채택완료

카페24, css다르게 주기에서,,

if (window.location.href.indexOf('키워드') > -1) {

     $('클래스명').addClass('active');

}

이 코드를 쓰고 있는데요,, 키워드 (예를들면 cate_no=해당페이지숫자)가 너무 많아서 일일이 한페이지씩 주니까 코드가 너무 많아집니다 혹시 이걸 하나로 묶어서 쓰는 방법 없을까요?

예를들면 

if (window.location.href.indexOf('cate_no=15~120') > -1) {

     $('클래스명').addClass('active');

}

뭐 이런식으로요! 일단 이렇게는 안되는데 설명드리기위해 이렇게 썼습니다

if (window.location.href.indexOf('cate_no=1') > -1) {

     $('클래스명').addClass('active');

}

if (window.location.href.indexOf('cate_no=2') > -1) {

     $('클래스명').addClass('active');

}

if (window.location.href.indexOf('cate_no=3') > -1) {

     $('클래스명').addClass('active');

}

if (window.location.href.indexOf('cate_no=4') > -1) {

     $('클래스명').addClass('active');

}

이런식으로 너무 많아집니다ㅜㅜ

|

답변 2개 / 댓글 2개

채택된 답변
+20 포인트
2021-12-21 (화) 13:07:01

if (window.location.href.indexOf('cate_no=' + i) > -1) { 

모바일로 작성하다 보니 앞에 if 를 빼먹었네요. ㅜㅠ

답변에 대한 댓글 1개

정말감사합니다ㅜㅜㅜ
2021-12-21 (화) 12:45:23

<script>

for (i = 15; i <= 120; i++) {

    window.location.href.indexOf('cate_no=' + i ) > -1) {

     $('클래스명').addClass('active');

    }

}

</script>

답변에 대한 댓글 1개

댓글 감사합니다

for (i = 15; i <= 120; i++) {

window.location.href.indexOf('cate_no=' + i ) > -1) {

$('클래스명').addClass('active');

}

}

근데 여기서 window.location.href.indexOf('cate_no=' + i ) > -1) { ← 요기 끝에 빨간줄이 생기면서 오류가 뜨는데 뭐가 잘못 되었을까요?!ㅜㅜ

답변을 작성하려면 로그인이 필요합니다.