When you click the href="home"
or href="cars"
ones, the browser follows the link, loading a completely new page. When you click ones that just have an anchor (href="#"
), that's navigation within the page, so the page isn't reloaded.
To highlight those navigation entries when the home
or cars
page loads, you'll need to run code on those new pages that finds and highlights them once the DOM is loaded.1
For instance, on the home
page:
$("ul li a[href=home]").addClass("current");
1 If you're targeting even semi-modern environments, you can have top-level code in a <script src="..." defer>
tag. In modern environments, you can use <script type="module">
instead. In old environments, just put the script
tag at the end of the body, just prior to </body>
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…