If you’re building a website with static pages, in HTML for example, the best way to do that is to add a class - linkAtivo
for example, in the navigation link corresponding to each designated page that is supposed to be the current page. In other words:
Let’s say we have a page sobre.html
. To highlight the navigation link to know that we are on the page sobre
, open the file sobre.html
and in the navigation of this page we will add the class linkAtivo
to the link about.
Page - sobre.html
<ul class="navegacao">
<li><a class="nav" href="/inicio.html">Início</a></li>
<li><a class="nav" href="/contacto.html">Contacto</a></li>
<li><a class="nav linkAtivo" href="/sobre.html">Sobre</a></li> <!-- Classe Adicionada -->
</ul>
And in CSS it will be the same for all pages:
.navegacao a.linkAtivo {
color: red;
}
Then just do the same on all pages. For example
Page contacto.html
:
<ul class="navegacao">
<li><a class="nav" href="/inicio.html">Início</a></li>
<li><a class="nav linkAtivo" href="/contacto.html">Contacto</a></li> <!-- Classe Adicionada -->
<li><a class="nav" href="/sobre.html">Sobre</a></li>
</ul>
And so on and so forth...
If you really want to use Javascript, you can get the same result as follows:
<script type="text/javascript">
for (var i = 0; i < document.links.length; i++) {
if (document.links[i].href == document.URL) {
document.links[i].className = 'linkAtivo';
}
}
</script>
Whenever someone clicks on a link it should generate a new request (a new page), so if you don’t dynamically do php you can insert a css class directly into the static page of your site. Ex: <li class="active" ><a href="#">home</a></li>
– Fernando Valler
Take a look at these links ai: http://www.maujor.com/tutorial/csslinks.php http://www.w3schools.com/cssref/sel_active.asp
– Fernando Valler
Thiago, if any answer helped mark as solution
– João Victor Gomes Moreira