开发者问题收集

出现错误“未捕获的类型错误:无法设置未定义的属性‘颜色’”[重复]

2020-09-28
1434

每当我单击按钮时,我都尝试将按钮文本的颜色更改为“白色”。但是单击按钮后,我收到错误“未捕获类型错误:无法设置未定义的属性‘颜色’”。我尝试使用 console.log(this) 提取对象,但它没有返回我单击的按钮的对象,而是返回“窗口”类型的对象。我不明白为什么我无法访问按钮并选择其中的文本并更改颜色。我正在粘贴我的 html 和 js 代码。请告诉我哪里出了问题。

for (var i = 0; i < (document.querySelectorAll('.drum').length); i++) {
  document.querySelectorAll(".drum")[i].addEventListener("click", () => {

    this.style.color = "white";

  });
}
body {
  text-align: center;
  background-color: #283149;
}

h1 {
  font-size: 5rem;
  color: #DBEDF3;
  font-family: "Arvo", cursive;
  text-shadow: 3px 0 #DA0463;
}

footer {
  color: #DBEDF3;
  font-family: sans-serif;
}

.w {
  background-image: url(./images/tom1.png);
}

.a {
  background-image: url(./images/tom2.png);
}

.s {
  background-image: url(./images/tom3.png);
}

.d {
  background-image: url(./images/tom4.png);
}

.j {
  background-image: url(./images/snare.png);
}

.k {
  background-image: url(./images/crash.png);
}

.l {
  background-image: url(./images/kick.png);
}

.set {
  margin: 10% auto;
}

.game-over {
  background-color: red;
  opacity: 0.8;
}

.pressed {
  box-shadow: 0 3px 4px 0 #DBEDF3;
  opacity: 0.5;
}

.red {
  color: red;
}

.drum {
  outline: none;
  border: 10px solid #404B69;
  font-size: 5rem;
  font-family: 'Arvo', cursive;
  line-height: 2;
  font-weight: 900;
  color: #DA0463;
  text-shadow: 3px 0 #DBEDF3;
  border-radius: 15px;
  display: inline-block;
  width: 150px;
  height: 150px;
  text-align: center;
  margin: 10px;
  background-color: white;
}
<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <title>Drum Kit</title>
  <link rel="stylesheet" href="styles.css">
  <link href="https://fonts.googleapis.com/css?family=Arvo" rel="stylesheet">
</head>

<body>

  <h1 id="title">Drum 🎼 Kit</h1>
  <div class="set">
    <button class="w drum">w</button>
    <button class="a drum">a</button>
    <button class="s drum">s</button>
    <button class="d drum">d</button>
    <button class="j drum">j</button>
    <button class="k drum">k</button>
    <button class="l drum">l</button>
  </div>


  <script src="index.js"></script>

</body>

</html>
2个回答

这是因为您使用了不绑定 this 的箭头函数,请参阅 w3c :

With arrow functions the this keyword always represents the object that defined the arrow function.

如果要绑定 this ,只需使用通常的 function() {}:

for (var i = 0; i < (document.querySelectorAll('.drum').length); i++) {
  document.querySelectorAll(".drum")[i].addEventListener("click", function() {

    console.log(this.innerHTML);

  });
}
body {
  text-align: center;
  background-color: #283149;
}

h1 {
  font-size: 5rem;
  color: #DBEDF3;
  font-family: "Arvo", cursive;
  text-shadow: 3px 0 #DA0463;
}

footer {
  color: #DBEDF3;
  font-family: sans-serif;
}

.w {
  background-image: url(./images/tom1.png);
}

.a {
  background-image: url(./images/tom2.png);
}

.s {
  background-image: url(./images/tom3.png);
}

.d {
  background-image: url(./images/tom4.png);
}

.j {
  background-image: url(./images/snare.png);
}

.k {
  background-image: url(./images/crash.png);
}

.l {
  background-image: url(./images/kick.png);
}

.set {
  margin: 10% auto;
}

.game-over {
  background-color: red;
  opacity: 0.8;
}

.pressed {
  box-shadow: 0 3px 4px 0 #DBEDF3;
  opacity: 0.5;
}

.red {
  color: red;
}

.drum {
  outline: none;
  border: 10px solid #404B69;
  font-size: 5rem;
  font-family: 'Arvo', cursive;
  line-height: 2;
  font-weight: 900;
  color: #DA0463;
  text-shadow: 3px 0 #DBEDF3;
  border-radius: 15px;
  display: inline-block;
  width: 150px;
  height: 150px;
  text-align: center;
  margin: 10px;
  background-color: white;
}
<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <title>Drum Kit</title>
  <link rel="stylesheet" href="styles.css">
  <link href="https://fonts.googleapis.com/css?family=Arvo" rel="stylesheet">
</head>

<body>

  <h1 id="title">Drum 🎼 Kit</h1>
  <div class="set">
    <button class="w drum">w</button>
    <button class="a drum">a</button>
    <button class="s drum">s</button>
    <button class="d drum">d</button>
    <button class="j drum">j</button>
    <button class="k drum">k</button>
    <button class="l drum">l</button>
  </div>


  <script src="index.js"></script>

</body>

</html>
johannchopin
2020-09-28

您之所以得到 undefined 是因为您使用了箭头函数,而 this 指的是 window 。解决方案是使用 event.target 代替 this

您可以使用匿名函数代替箭头函数,并在其中使用 this 来实现相同的效果。

var all = document.querySelectorAll(".drum");

for (var i = 0; i < all.length; i++) {
  all[i].addEventListener("click", (event) => {
    console.log(event.target.innerHTML);
    event.target.style.color = "#fff";
  });
}
body {
  text-align: center;
  background-color: #283149;
}

h1 {
  font-size: 5rem;
  color: #DBEDF3;
  font-family: "Arvo", cursive;
  text-shadow: 3px 0 #DA0463;
}

footer {
  color: #DBEDF3;
  font-family: sans-serif;
}

.w {
  background-image: url(./images/tom1.png);
}

.a {
  background-image: url(./images/tom2.png);
}

.s {
  background-image: url(./images/tom3.png);
}

.d {
  background-image: url(./images/tom4.png);
}

.j {
  background-image: url(./images/snare.png);
}

.k {
  background-image: url(./images/crash.png);
}

.l {
  background-image: url(./images/kick.png);
}

.set {
  margin: 10% auto;
}

.game-over {
  background-color: red;
  opacity: 0.8;
}

.pressed {
  box-shadow: 0 3px 4px 0 #DBEDF3;
  opacity: 0.5;
}

.red {
  color: red;
}

.drum {
  outline: none;
  border: 10px solid #404B69;
  font-size: 5rem;
  font-family: 'Arvo', cursive;
  line-height: 2;
  font-weight: 900;
  color: #DA0463;
  text-shadow: 3px 0 #DBEDF3;
  border-radius: 15px;
  display: inline-block;
  width: 150px;
  height: 150px;
  text-align: center;
  margin: 10px;
  background-color: white;
}
<link href="https://fonts.googleapis.com/css?family=Arvo" rel="stylesheet">

<h1 id="title">Drum 🎼 Kit</h1>
<div class="set">
  <button class="w drum">w</button>
  <button class="a drum">a</button>
  <button class="s drum">s</button>
  <button class="d drum">d</button>
  <button class="j drum">j</button>
  <button class="k drum">k</button>
  <button class="l drum">l</button>
</div>
Pranav Rustagi
2020-09-28