无法读取未定义的属性(读取’addEventListener’)?如何解决这个问题?

huangapple go评论72阅读模式
英文:

Cannot read properties of undefined (reading 'addEventListener')? how to solve this issue?

问题

抱歉,我不能执行你的请求。如果你有任何其他问题或需要帮助,请随时告诉我。

英文:
  • Can anyone help to solve this issue I can't get what wrong with code !!!!
  1. This one is javascript code

  2. Html code

  3. Error shows in console

答案1

得分: 1

.querySelector(".drum")选择第一个具有类名"drum"的元素,所以{i}是无用的,应该使用querySelectorAll。

英文:

.querySelector(".drum") selects the first element with class "drum" so the {i} is useless, use querySelectorAll.

答案2

得分: 0

正如在这篇帖子中提到的: https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors/285557#285557,你不应该上传代码的图像。当你在StackOverflow上提问题时,应该优先使用代码标记嵌入你的代码。这样,其他人可以更容易地回答你。

在你的情况下,你正在使用不良的JavaScript函数,如果你想选择所有具有"drum"类的按钮,应该使用querySelectorAll来替代querySelector

querySelector会选择并返回条件的第一个匹配项,正如文档中所述: https://developer.mozilla.org/fr/docs/Web/API/Document/querySelector

英文:

As it mentioned in this post: <https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors/285557#285557>
, you should not upload image of your code. You should prefer embed your code with code tag when you ask question on stackoverflow.
In this way, people can answer you more easily.

In your case your using bad javascript function, you should use querySelectorAll in place of querySelector if you want to select all your button that have the drum class.

querySelector select and return the first match of the condition as it said in the documentation: <https://developer.mozilla.org/fr/docs/Web/API/Document/querySelector>

huangapple
  • 本文由 发表于 2023年2月16日 18:45:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75471133.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定