简体   繁体   English

为 eventListener 访问 javaScript 中的数组元素

[英]Accessing array elements in javaScript for an eventListener

I am having trouble getting the div elements to change their background colours onmouseover to the colours set in the array colours.我无法让 div 元素在鼠标悬停时将其背景颜色更改为数组颜色中设置的颜色。 I would like it to accomplish the same thing that I would when you replace the line this.style.background = colours[i] with the one below it我希望它完成与将 this.style.background = colours[i] 行替换为它下面的行时相同的事情

 //An array of different colours var colour = ['blue', 'green', 'yellow', 'purple', 'peach', 'pink', 'orange', 'magenta', 'black', 'aliceblue']; var divContainer = document.getElementById('container'); var divS = divContainer.getElementsByClassName('innerDiv'); for (var i = 0; i < divS.length; i++) { divS[i].addEventListener('mouseover', function() { this.style.background = colour[i]; //this.style.background = "gold"; }); divS[i].addEventListener('mouseout', function() { this.style.background = "white"; }); }
 #container{ height: 100vh; }.innerDiv { height: 10%; width: 100%; }
 <.DOCTYPE html> <html lang="en"> <head> <title>Understanding JavaScript</title> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <div id="container"> <div class="innerDiv">Hover me to change the colour blue</div> <div class="innerDiv">Hover me to change the colour green</div> <div class="innerDiv">Hover me to change the colour yellow</div> <div class="innerDiv">Hover me to change the colour purple</div> <div class="innerDiv">Hover me to change the colour peach</div> <div class="innerDiv">Hover me to change the colour pink</div> <div class="innerDiv">Hover me to change the colour orange</div> <div class="innerDiv">Hover me to change the colour magenta</div> <div class="innerDiv">Hover me to change the colour black</div> <div class="innerDiv">Hover me to change the colour aliceblue</div> </div> <!--Link to a script--> <script type="text/javascript" src="script.js"></script> </body> </html>

your problem is about the i of colour[i] because it is undefined when the event become.你的问题是关于colour[i]i因为事件发生时它是未定义的。

change your code from更改您的代码

for (var i = 0; i < divS.length; i++)

to

for (let i = 0; i < divS.length; i++)
// DOM (Document Object Model) fully Loaded and parsed
window.addEventListener('DOMContentLoaded', (event) => {
// Changed Peach to PeachPuff & aliceblue to AliceBlue to match HTML Color codes. Note that the AliceBlue might not be fully visible to distinguish between it and the whit background colour
var colour = ['blue', 'green', 'yellow', 'purple', 'PeachPuff', 
            'pink', 'orange', 'magenta', 'black', 'AliceBlue'];

var divContainer = document.getElementById('container');
// Used query selectorAll to return array of nodes with class name. And the . before the class name
var divS = document.querySelectorAll('.innerDiv');

for (let i = 0; i < divS.length; i++)
{
    console.log(divS[i]);
    divS[i].addEventListener('mouseover', function() {
        this.style.background = colour[i];

        
    });
    divS[i].addEventListener('mouseout', function() {
        this.style.background = "white";
    });
}
});

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

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