I have this menu:
<div id = "menu" >
<a href = "index.html"> <img src = "images/logo.png" style = "padding-left: 5%; padding-top: 1%;"> </a>
<ul id = "lista">
<li style = "display: inline-block;"> <a href = "aboutus.html"> <padding left> SOBRE O INSTITUTO </a></li>
<li style = "display: inline-block;"> <a href = "pessoas.html"> PESSOAS</a> </li>
<li style = "display: inline-block;"> <a href = "universidadefour.html"> UNIVERSIDADE FOUR </a>
<ul>
<a href = "programaprolider.html"> <li> PROGRAMA PROLÍDER </li> </a>
<a href = "universidadefour.html#escolapolitica"> <li> ESCOLA DE POLÍTICA </li> </a>
<a href = "universidadefour.html#escolaempreendedorismo"> <li> ESCOLA DE EMPREENDEDORISMO </li> </a>
</ul> </li>
<li style = "display: inline-block;"> <a href = "comunidadefour.html"> <padding left> COMUNIDADE FOUR </a>
<ul>
<a href = "comunidadefour.html"> <li> A COMUNIDADE FOUR</li> </a>
<a href = "comunidade.html"> <li> FELLOWS </li> </a>
</ul>
</li>
<li style = "display: inline-block;"> <a href = "foursummit.html"> FOUR SUMMIT </a> </li>
<li style = "display: inline-block;"> <a href = "trabalheconosco.html"> VAGAS ABERTAS</a> </li>
<li style = "display: inline-block;"> <a href = "en-index.html"> EN </a> <span style = "color: white;"> |</span> <a href = "index.html"> BR </a> </li>
</ul>
</div>
And these are the CSS specifications for the color of the links:
#menu ul li a{
text-decoration: none;
line-height: 70px;
font-size: 14px;
padding: 4px 15px 4px 15px;
color: white;
}
This menu doesn't have any specified background color. However, after the user scrolls around the page, it changes:
window.onscroll = function() {scrollFunction()};
function scrollFunction() {
if (document.body.scrollTop > 80 || document.documentElement.scrollTop > 80) {
document.getElementById("menu").style.backgroundColor = "#fff";
} else {
document.getElementById("menu").style.backgroundColor = "";
}
}
I want to do the same thing with this menu's link colors. How can I do it, change the links colors from white to #333333?
Thanks!
use document.querySelectorAll()
to select the elements with the specified selector. Learn More
Inside your function, within the if(){}
block,
var links = document.querySelectorAll("#lista a"); // selects all <a> inside #lista
links.forEach((link)=>link.style.color = "#333333"); // loop through each link and change the color
You can use getElementsByTagName() to select elements with specified tag and then change color of link one by one.
if (document.body.scrollTop > 80 || document.documentElement.scrollTop > 80) {
var y = document.getElementsByTagName("a"); //Select all <a> tags
for (i = 0; i < y.length; i++) {
y[i].style.color = "#333333"; //Loop through links array and change the link color
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.