我正在尝试将事件侦听器添加到网站上的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>
2条答案
按热度按时间yyhrrdl81#
navbarparagraphelements是元素列表。你必须为每一个单独设置。
ergxz8rk2#
在我看来,最好使用单个css类来切换亮/暗