【问题标题】:Cannot read property 'backgroundColor' of undefined无法读取未定义的属性“背景颜色”
【发布时间】:2017-02-17 02:02:12
【问题描述】:

当下面的函数调用运行时,它会返回此错误。 “未捕获的类型错误:无法读取属性 'backgroundColor' undefined" 我正在尝试更改名为的类的背景颜色 .jumbotron。到目前为止,我已经尝试了所有我能做的事情。

Could anyone tell me why this is happening?

clrElementJumbo("cornsilk");

function clrElementJumbo(scolor) {
  var el = document.getElementsByClassName("jumbotron");
  el.style.backgroundColor = scolor;
}

【问题讨论】:

    标签: properties undefined


    【解决方案1】:

    尝试替换代码

           var el = document.getElementsByClassName("jumbotron");
    

    以下

           var el=document.getElementsByClassName("jumbotron")[0];
    

    【讨论】:

      猜你喜欢
      • 2016-06-02
      • 2022-08-06
      • 2018-01-16
      • 2020-08-17
      • 2019-01-25
      • 2020-12-14
      • 2021-11-01
      • 2018-08-17
      • 1970-01-01
      相关资源
      最近更新 更多