i am making a navigation bar and want the class `active to be added when i am on that specific page
code bellow is not ruining with the if statement (shown with comment)
<html>
<head>
<style>
.active {
color: red;
}
</style>
</head>
<body>
<a href="a.html" class="nav-item">nav</a>
<a href="b.html" class="nav-item">nav</a>
<p id="z">aaa</p>
<p id="zz"></p>
<script>
var navClass = document.getElementsByClassName("nav-item");
var path = window.location.href;
for (i = 0; i < navClass.length; i++) {
//my issue
if (navClass[i] === path) {
navClass[i].classList.add("active");
}
}
//for testing
var xx = navClass[0];//.getAttribute("herf");
document.getElementById("zz").innerHTML = xx + "<br>" + path;
</script>
</body>
</html>
Aucun commentaire:
Enregistrer un commentaire