I have a navigation menu which changes color when you have the mouse over one of the links/elements in the menu. Each link / list item runs a javascript function when you click on it which will hide som content and some other content will be displayed on the page. At the same time, the background color changes for the page / link which you are visiting. It works as it should but the problem is that once you have clicked on one of the links, the css code ul.nav li:hover does not work anymore.
Here is my code so far:
function show_page1() {
let page1 = document.querySelector("#page1");
let page2 = document.querySelector("#page2");
let page1_Link = document.querySelector("#page1_link");
let page2_Link = document.querySelector("#page2_link");
page2.style = "display: none";
page1.style = "display: block";
page1_Link.style = "background-color: #008CBA";
page2_Link.style = "background-color: #f3f3f3";
}
function show_page2() {
let page1 = document.querySelector("#page1");
let page2 = document.querySelector("#page2");
let page1_Link = document.querySelector("#page1_link");
let page2_Link = document.querySelector("#page2_link");
page2.style = "display: block";
page1.style = "display: none";
page1_Link.style = "background-color: #f3f3f3";
page2_Link.style = "background-color: #008CBA";
}
ul.nav {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #f3f3f3;;
line-height: 1.5;
}
ul.nav li {
float: left;
display: inline-block;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
ul.nav li:hover {
background-color: lightgrey;
cursor: pointer;
}
#page1_link {
background-color: #008CBA;
}
#page2 {
display:none;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<nav>
<ul class="nav">
<li id="page1_link" onclick="show_page1()">page 1</li>
<li id="page2_link" onclick="show_page2()">page 2</li>
</ul>
</nav>
<div id="page1">
Page 1
</div>
<div id="page2">
Page 2
</div>
</body>
</html>