D
D
dez64ru2015-09-19 18:11:39
JavaScript
dez64ru, 2015-09-19 18:11:39

How to check the active page using JS?

I tried to make a navigation menu using JS, but the script does not want to work.
In Js, so far, a complete zero.

<div class="header">
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".menu li a").each(function() {
    if ($(this).attr("href") == location.href) {
        $(this).addClass("selected");
    }
})
});
</script>
<style>
.selected {
  color: #fff;
}
</style>
<ul class="menu">
<li><a href="/">Главная</a></li>
<li><a href="inf.html">О нас</a></li>
<li><a href="photo.html">Фотогалерея</a></li>
<div class="l1"></div>
</ul>
</div>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Azim Kurt, 2015-09-19
@Symphony

location.href returns a full path like https://toster.ru/q/249989 , you should use location.pathname
Good tutorial if you want to learn js and not make that kind of mistake.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question