用Javascript改变所有<a>标签的颜色。

目前,我只能用Javascript更改列表中的第一个标签。a 列表中的标签应该变成黑色,但只有第一个 a 列表中的标签似乎受到了影响。最终结果应该是向上滚动时 a 标签(字母)变成黑色,向下滚动时变成黑色背景的白色。

超文本标记语言

<header>
  <nav>
    <div class="menu-container" id="nav-menu">
      <!-- <a href="Home.html"><img class="logo" src="pics/logo.png"  width="50" height="50" alt="logo"></a> -->
      <div id="nav-class" class="aw-burger-open">
      <ul class="menu-navigation">
        <li><a class="sec section-home" href="#">Home</a></li>
        <li><a class="sec section-contact" href="#">Productivity Hacks</a></li>
        <li><a class="sec section-podcasts" href="#">Podcasts</a></li>
        <li><a class="sec section-books" href="#">Books</a></li>
        <li><a class="sec section-latest-posts" href="#">Latest Posts</a></li>
        <li><a class="sec section-contact" href="#">Get in touch</a></li>
        <li><a class="sec section-newsletter" href="#">Subscribe</a></li>
      </ul>
    </div>
      </div>
      </nav>

CSS

.menu-container img{
  margin-top: 20px;
  margin-left: 20px;
}
header nav {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1000;
  width: 100%;
}
.menu-container{
  display: flex;
  flex-direction: row;
  justify-content: center;
}
.menu-navigation {
  display: flex;
  list-style-type: none;
  padding-bottom: 10px;
}

#nav-class{
  padding-top: 30px;
}
#nav-class > ul > li:nth-child(7) > a{
  border: 2px solid white;
  border-radius: 18px;
  padding:7px;
}

#nav-class > ul > li:nth-child(7) > a:hover{
  background-color: white;
  color:black;
}

header nav ul.menu-navigation li a {
  padding: 0 16px;
  font-size: 14px;
}



a:active {
  text-decoration:none;
}

.menu-container a:hover{
  color:#d5c8bb ;
}

a:link{
    text-decoration:none;
    color:white;
}

JS

var nav = document.getElementById('nav-menu')
var letters = document.querySelector('#nav-class > ul > li > a')

window.onscroll = function(){
    if(window.pageYOffset > 100){
        nav.style.background = 'black'
        letters.style.color = 'white'
    } else{
        nav.style.background = '#eaebeb'
        letters.style.color = 'black'

    }
}

解决方案:

按照评论中的建议,修正了你的代码。querySelectorAll 而不是 querySelector 会解决你的问题。还需要在 .nav-class.

var nav = document.getElementById("nav-menu");
var letters = document.querySelectorAll("#nav-class > ul > li > a");

window.onscroll = function () {
  if (window.pageYOffset > 100) {
    nav.style.background = "black";
    letters.forEach((letter) => (letter.style.color = "white"));
  } else {
    nav.style.background = "#eaebeb";
    letters.forEach((letter) => (letter.style.color = "black"));
  }
};
.menu-container img {
  margin-top: 20px;
  margin-left: 20px;
}
header nav {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1000;
  width: 100%;
}
.menu-container {
  display: flex;
  flex-direction: row;
  justify-content: center;
}
.menu-navigation {
  display: flex;
  list-style-type: none;
  padding-bottom: 10px;
}

#nav-class {
  padding-top: 30px;
}
#nav-class > ul > li:nth-child(7) > a {
  border: 2px solid white;
  border-radius: 18px;
  padding: 7px;
}

#nav-class > ul > li:nth-child(7) > a:hover {
  background-color: white;
  color: black;
}

header nav ul.menu-navigation li a {
  padding: 0 16px;
  font-size: 14px;
}

a:active {
  text-decoration: none;
}

.menu-container a:hover {
  color: #d5c8bb;
}

a:link {
  text-decoration: none;
  color: white;
}

body {
  min-height: 1000px;
}
<header>
  <nav>
    <div class="menu-container" id="nav-menu">
      <!-- <a href="Home.html"><img class="logo" src="pics/logo.png"  width="50" height="50" alt="logo"></a> -->
      <div id="nav-class" class="aw-burger-open">
        <ul class="menu-navigation">
          <li><a class="sec section-home" href="#">Home</a></li>
          <li><a class="sec section-contact" href="#">Productivity Hacks</a></li>
          <li><a class="sec section-podcasts" href="#">Podcasts</a></li>
          <li><a class="sec section-books" href="#">Books</a></li>
          <li><a class="sec section-latest-posts" href="#">Latest Posts</a></li>
          <li><a class="sec section-contact" href="#">Get in touch</a></li>
          <li><a class="sec section-newsletter" href="#">Subscribe</a></li>
        </ul>
      </div>
    </div>
  </nav>

本文来自投稿,不代表实战宝典立场,如若转载,请注明出处:https://www.shizhanbaodian.com/17157.html

(0)
上一篇 1天前
下一篇 1天前

相关推荐

发表评论

登录后才能评论