a태그의 href 필터 가능한가요~? 채택완료
안녕하세요. 초보 퍼블리셔입니다 !ㅎㅎ
다름이 아니라~ 페이지 별로 다른 클래스명에 active를 주는것은 성공했습니다~
<script>
$(document).ready(function () {
var url = window.location.href;
$('#header .gnb .depth2 > a').filter(function () {
if (url.includes('sub01.php')) {
$(this).parent(".depth1").addClass('active');
} else if (url.includes('sub02.php')) {
$(this).parent(".depth1").addClass('active');
}
})
});
</script>
이렇게요!
현재는 url 주소를 통한 클래스명을 추가를 사용중인데요
a태그에 href를 통한 클래스명 추가를 하고싶습니다 ( url 말구요 ! )
방법이 있을까요?
답변 2개
답변에 대한 댓글 5개
$(document).ready(function () {
var url = window.location.href;
$('#header .gnb .depth2 > a').filter(function () {
if url.includes($(this).attr('href')) {
$(this).parent(".depth2").addClass('active');
} else if url.includes($(this).attr('href')) {
$(this).parent(".depth2").addClass('active');
}
})
});
</script>
<a href="~/sub01.php">서브1</a> 로 href 에 주소가 있어야 됩니다.
<script>
$(document).ready(function () {
var url = window.location.href;
$('#header .gnb .depth2 > a').filter(function () {
if url.includes($(this).attr('/theme/a01/sub01/sub01.php')) {
$(this).parent(".depth2").addClass('active');
} else if url.includes($(this).attr('/theme/a01/sub01/sub02.php')) {
$(this).parent(".depth2").addClass('active');
}
})
});
</script>
a태그에는 링크가 잘 들어있습니다만.. 여젼히 클래스명에 active는 추가되지않습니다 ㅠ
$(document).ready(function () {
var url = window.location.href;
$('#header .gnb .depth2 > a').filter(function () {
if url.includes($(this).attr('href')) {
$(this).parent(".depth2").addClass('active');
}
});
});
</script>
or
<script>
$(document).ready(function () {
var url = window.location.href;
$('#header .gnb .depth2 > a').each(function () {
if url.includes($(this).attr('href')) {
$(this).parent(".depth2").addClass('active');
}
});
});
</script>
$(this).attr('href')
=> a 태그의 href 속성값 입니다.
댓글을 작성하려면 로그인이 필요합니다.
* if 다음에 '(' 가 빠져서 추가하였습니다.
* includes 대신에 제가 주로 사용한 방식인 indexOf 로 수정해 보았습니다.
</p>
<p><script>
$(document).ready(function () {
var url = window.location.href;</p>
<p> $('#header .gnb .depth2 > a').each(function () {
if (url.indexOf($(this).attr('href')) > -1) {
$(this).parent(".depth2").addClass('active');
}
});
});
</script></p>
<p>
답변에 대한 댓글 1개
댓글을 작성하려면 로그인이 필요합니다.
답변을 작성하려면 로그인이 필요합니다.
로그인
$(document).ready(function () {
$('#header .gnb .depth2 > a').filter(function () {
if url.includes($(this).attr('sub01')) {
$(this).parent(".depth2").addClass('active');
} else if url.includes($(this).attr('sub02')) {
$(this).parent(".depth2").addClass('active');
}
})
});
</script>
이런식으로 시도해봤는데 되지않네요..ㅠ 뭐가문제일까요??
http://iden07.cafe24.com/theme/a01/sub01/sub01.php