简体   繁体   English

JavaScript 事件侦听器不使用 if 语句

[英]JavaScript event listener is not working with if statement

I have a container div with many smaller divs inside of it, each with a class of box.我有一个容器 div,里面有许多较小的 div,每个都有一个 class 的盒子。 The container is styled as a grid.容器的样式为网格。 I have an event listener that changes the background color of the grid boxes when there is a mouseover event.我有一个事件侦听器,当出现鼠标悬停事件时,它会更改网格框的背景颜色。

I want to make a psychedelic mode that makes the boxes a random color when there is a mouse over instead of a fixed color.我想制作一种迷幻模式,当鼠标悬停在上面而不是固定颜色时,使盒子成为随机颜色。

I have a button:我有一个按钮:

 <button class="psychMode">Psychedelic Mode</button>

It is identified in the JavaScript:在 JavaScript 中标识:

let psych = document.querySelector('.psychMode');
let psychClass = psych.getAttribute('class');

I have a toggle on the class of the button to change it to psychMode active when the button is clicked:我在按钮的 class 上有一个切换按钮,可以在单击按钮时将其更改为psychMode active状态:

psych.addEventListener('click', function () {
    psych.classList.toggle('active');
})

Finally, I have a function that returns a random number and the event listeners for the boxes:最后,我有一个 function 返回一个随机数和盒子的事件监听器:

function randomColor() {
    let num= Math.floor(Math.random() * 255);
    return num;
}

let boxes = document.querySelectorAll('.box')

for (let i of boxes) {
    i.addEventListener('mouseover', () => {
        i.style['backgroundColor'] = 'black'
        i.style.border = '1px solid red'
    })
}

for (let i of boxes) {
    if (psychClass === 'active') {
        i.addEventListener('mouseover', () => {
            i.style['backgroundColor'] = `rgb(${randomColor()},${randomColor()},${randomColor()})`
            i.style.border = `1px solid rgb(${randomColor()},${randomColor()},${randomColor()})`
        })
    }
}

For some reason, the code only results in a black background and red border.出于某种原因,该代码只会导致黑色背景和红色边框。 It doesn't choose a random color even when the active class is toggled on.即使打开活动 class,它也不会选择随机颜色。 I also tried putting both conditionals in the for loop together, it still didn't work.我也试过将两个条件放在一起 for 循环,它仍然没有用。 Can someone tell me what I'm doing wrong?有人可以告诉我我做错了什么吗?

for (let i of boxes) {
    i.addEventListener('mouseover', () => {
        if(!psych.classList.contains("active"){
          i.style.backgroundColor = 'black'
          i.style.border = '1px solid red'
        }else{
          i.style.backgroundColor = `rgb(${randomColor()},${randomColor()},${randomColor()})`
          i.style.border = `1px solid rgb(${randomColor()},${randomColor()},${randomColor()})`
        }
    })
}

use this instead of those 2 for loops.使用它而不是那些 2 for 循环。

make your code as concise as possible and avoid unnecessary loops.使您的代码尽可能简洁并避免不必要的循环。

You're only ever assigning psychClass once, so it always equals "psychMode" .您只分配一次psychClass ,因此它始终等于"psychMode"

You'd be better off ditching that variable altogether and going with Element.classList.contains (for the specifics of the contains function, see here )您最好完全放弃该变量并使用Element.classList.contains (有关contains function 的详细信息, 请参见此处

function randomColor() {
    let num= Math.floor(Math.random() * 255);
    return num;
}

let boxes = document.querySelectorAll('.box')

for (let i of boxes) {
    i.addEventListener('mouseover', () => {
        i.style['backgroundColor'] = 'black'
        i.style.border = '1px solid red'
    })
}

for (let i of boxes) {
    if (psych.classList.contains('active')) {
        i.addEventListener('mouseover', () => {
            i.style['backgroundColor'] = `rgb(${randomColor()},${randomColor()},${randomColor()})`
            i.style.border = `1px solid rgb(${randomColor()},${randomColor()},${randomColor()})`
        })
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM