queryselectorall元素不更改选定类的颜色

b5buobof  于 2021-09-23  发布在  Java
关注(0)|答案(2)|浏览(289)

我正在尝试将事件侦听器添加到网站上的html元素中。讨论中的元素使用类名将它们分组在一起。但是,当我尝试单击进行更改的元素时,我的事件侦听器不起作用。以下是相关代码:

// page elements for bright/dark mode

let brightOrDarkModeButton = document.querySelector('.sun-img-container')
let navBar = document.querySelector('.navBar')
let navBarParagraphElements = document.querySelectorAll('.nav-menu-option');

//

// DOM Events

brightOrDarkModeButton.addEventListener('click', changeBackgroundColour = () => {
  navBar.style.transition = 'background-color 0.15s linear';
  navBar.style.backgroundColor = 'white';
  navBarParagraphElements.style.transition = 'color 0.15s linear';
  for (let i = 0; i < navBarParagraphElements.length; i++) {
    navBarParagraphElements[i].style.color = 'black';
  }
});
<nav class="navBar">
  <a class='BP' href="REMOVED FOR PRIVACY">BP</a>
  <div class="nav-menu">
    <a href="#" class="nav-menu-option">Home</a>
    <a href="#about-me" class="nav-menu-option">About Me</a>
    <a href="#projects" class="nav-menu-option">Projects</a>
  </div>
  <a class="CV-container" href="REMOVED FOR PRIVACY">
    <p class="CV">CV</p>
  </a>
  <div class="sun-img-container">
    <img src="images/Sun.png">
  </div>
</nav>
yyhrrdl8

yyhrrdl81#

navbarparagraphelements是元素列表。你必须为每一个单独设置。

// page elements for bright/dark mode

let brightOrDarkModeButton = document.querySelector('.sun-img-container')
let navBar = document.querySelector('.navBar')
let navBarParagraphElements = document.querySelectorAll('.nav-menu-option');

//

// DOM Events

brightOrDarkModeButton.addEventListener('click', changeBackgroundColour = () => {
    navBar.style.transition = 'background-color 0.15s linear';
    navBar.style.backgroundColor = 'white';
    for (let i = 0; i < navBarParagraphElements.length; i++) {
        navBarParagraphElements[i].style.color = 'black';
        navBarParagraphElements[i].style.transition = 'color 0.15s linear'; //Add this here
    }
});
<nav class="navBar">
  <a class="BP">BP</a>
  <div class="nav-menu">
    <a href="#" class="nav-menu-option">Home</a>
    <a href="#about-me" class="nav-menu-option">About Me</a>
    <a href="#projects" class="nav-menu-option">Projects</a>
  </div>
  <a class="CV-container">
    <p class="CV">CV</p>
  </a>
  <div class="sun-img-container">
    <img src="images/Sun.png" />
  </div>
</nav>
ergxz8rk

ergxz8rk2#

在我看来,最好使用单个css类来切换亮/暗

// page elements for bright/dark mode

let brightOrDarkModeButton = document.querySelector('.sun-img-container')
let navBar = document.querySelector('.navBar')

//

// DOM Events

brightOrDarkModeButton.addEventListener('click', () => navBar.classList.toggle("dark"));
.navBar.dark {
  transition: background-color 0.15s linear;
  background-color: white;
}

.navBar.dark .nav-menu-option {
  transition: color 0.15s linear;
  color: black;
}
<nav class="navBar">
  <a class='BP' href="REMOVED FOR PRIVACY">BP</a>
  <div class="nav-menu">
    <a href="#" class="nav-menu-option">Home</a>
    <a href="#about-me" class="nav-menu-option">About Me</a>
    <a href="#projects" class="nav-menu-option">Projects</a>
  </div>
  <a class="CV-container" href="REMOVED FOR PRIVACY">
    <p class="CV">CV</p>
  </a>
  <div class="sun-img-container">
    <img src="images/Sun.png">
  </div>
</nav>

相关问题