In this thread "How to change the link color of the current page with CSS" @Taraman writes some neat JQuery function that is praised by others. Now I tried to use his code, but unfortunately it didn't work for me. I was busting my brains out to figure out why his code doesn't work for me, but I couldn't figure it out.
I would be very grateful if someone could explain me how his code works and what I'm missing.
Here is my html code:
<html>
<head>
<script type="text/javascript" src="../js/jquery.js"></script>
<script> <!--Taraman's code-->
$(document).ready(function() {
$("[href]").each(function() {
if (this.href == window.location.href) {
$(this).addClass("active");
}
});
});
</script>
</head>
<body>
<div id="topnav-bg">
<div id="topnav">
<ul>
<li><a href="../index.html">Home</a></li> <!--dots mean "www.myurlsomething.com"-->
<li><a href="../about.html">About</a></li>
<li><a href="../science.html">Science</a></li>
<li><a href="../adventure.html">Field trip</a></li>
<li><a href="../team.html">Team</a></li>
<li><a href="../biblio.html">Bibliography</a></li>
</ul>
</div>
</div>
</body>
</html>
And below is my css code:
#topnav-bg {clear:both; background-color:#FAF7C0; }
#topnav ul { margin: 10px 0; padding: 3px 0; }
#topnav ul li { display:inline; }
#topnav ul li a { padding: 0 9px; font: bold 14px Verdana; }
#topnav a:link { color: black; }
#topnav a:visited { color: black; }
#topnav a:hover { color: grey; }
#topnav a:active { color: blue; }
#topnav a:focus { color: blue; }
.active { color: blue; font-weight: bold;}
So what I would like to achieve with my code is that when I'm on "About" page, link/word About is coloured blue, while others (Home, Science, ...) are black, etc.
This is probably "no-brainer" for some of you, but I'm obviously missing something.
I would like to know what this line $("[href]").each(function()
does? Should "[href]"
be changed by something else?
This line if (this.href == window.location.href)
returns page location, right? So in my case this should be "www.myurlsomething.com/about.html, right?
Taraman is also mentioned something about url parameters if (this.href.split("?")[0] == window.location.href.split("?")[0]) ...
. What this code does and to what url parameters was he referring? Should I use this?
I know this is a lot of questions, probably stupid questions. But like they say: "There is no such thing, as a stupid question." :)
Thank you for your help!