javascript - 控制台抛出语法“未捕获的类型错误:note.addEventListener 不是函数”?
问题描述
我想做一个钢琴键手。我已经成功完成了 html 和 css 编码,但是在 javascript 中我遇到了许多我实际上不知道的问题。
在浏览器控制台中抛出这些语法:
Uncaught TypeError: note.addEventListener is not a function;
Reference Error: notes is not defined;
const keys = ['c-key', 'd-key', 'e-key', 'f-key', 'g-key', 'a-key', 'b-key', 'c2-key', 'c-sharp-key', 'd-sharp-key', 'f-sharp-key', 'g-sharp-key', 'a-sharp-key'];
const notes = [];
//for each loop to take the target dom
keys.forEach(function(key) {
notes.push(document.getElementsByClassName(key))
});
//named function that change the color of the target key
const keyColor = function(event) {
event.target.style.color = 'green';
};
const keyReturn = function(event) {
event.target.style.color = '';
}
//function handler event listener;
const eventAssignment = function(note) {
note.addEventListener('mousedown', keyColor);
note.addEventListener('mouseup', keyReturn);
};
//run array element through for each loop
notes.forEach(eventAssignment);
我在jsbin中的文件在这里
解决方案
推荐阅读
- sparql - 有没有办法使用 Blazegraph 中的索引有效地执行 ORDER BY + LIMIT ?
- c++ - Magick++ Image.GetPixels 在多次成功迭代后返回 nullptr
- python - 球与物体碰撞
- google-sheets - =GOOGLEFINANCE("AAPL";"eps";TODAY();今天()-60)
- python - Django:使用 Django 租户创建主站点
- sql - SQL查询透视,将值移到顶部
- php - 如何在 laravel 的 latest() 中使用 ASC 和 DESC
- java - 如何将我的“for”循环变成“for of”循环?
- c - 为单链表实现合并排序?
- javascript - webpack注入脚本后如何加载脚本标签?