【问题标题】:JavaScript Failed to execute 'getComputedStyle' on 'Window': parameter 1 is not of type 'Element'JavaScript 无法在“窗口”上执行“getComputedStyle”:参数 1 不是“元素”类型
【发布时间】:2019-12-11 03:57:17
【问题描述】:

我试图让一个小方块在单击时消失,但前提是它是错误的选择。

问题是我希望它慢慢消失,我在选择正方形时遇到了一些问题。

for (var i = 0; i < difficulty; i++) {
  card[i].addEventListener("click", function(){
    if(this.classList.contains("rightGuess")){
      victoryPar.innerHTML = "You're right!"
    }
    else{
      victoryPar.innerHTML = "Try again"
      this.classList.add("wrong");
      var cartaSbagliata = document.getElementsByClassName("wrong")[incremento];
      opacityWrong = Number(window.getComputedStyle("cartaSbagliata").getPropertyValue("opacity"));
      incremento++;
      interId = setInterval(function(){
        if(opacityWrong > 0){
          opacityWrong = opacityWrong-0.1;
          cartaSbagliata.style.opacity = opacityWrong
        }
        else{
          clearInterval(interID);
        }
      }, 40)

    }
  })
}

我只是不知道如何在getComputedStyle 中选择card[i]。 有点乱,因为我几乎尝试了所有东西,所以如果有人能向我解释如何正确选择 getComputedStyle 的项目,那就太棒了。

我是初学者,请解释一下,否则我什么都不懂。

【问题讨论】:

  • 你试过用 event.target 代替 this 吗?
  • 是的,尝试了 rn,但每个“this”都表现良好,我不明白如何从 getComputedStyle 中调用 card[i]。
  • 酷,现在解决你的问题,而不是给 getComputedStyle 一个字符串,给函数一个元素。像getComputedStyle(document.getElementById("cartaSbagliata")) 这样的东西。甚至是this
  • 如果您可以edit 您的问题和其中的 sn-p,添加运行代码的 HTML,minimal reproducible example,这将极大地帮助我们找出问题所在继续。
  • 完成了老板,谢谢大家的帮助,太棒了

标签: javascript this fade getcomputedstyle


【解决方案1】:

正如错误所说

参数 1 不是“元素”类型

"cartaSbagliata" 是一个字符串,而不是一个元素。

你在变量cartaSbagliata中有一个元素,所以使用它:

window.getComputedStyle(cartaSbagliata)

【讨论】:

  • 谢谢!这清除了我对这个问题的想法,现在我得到一个“无法设置未定义的属性'不透明度'”但这是向前迈出的一步,我不再感到卡住了(我猜啊哈)
猜你喜欢
  • 2017-12-27
  • 1970-01-01
  • 2022-06-23
  • 2021-08-24
  • 1970-01-01
  • 2015-08-28
  • 2018-03-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多