无法读取未定义的属性'backgroundColor'

时间:2016-03-27 07:04:20

标签: properties undefined

当下面的函数调用运行时,它返回此错误。  “未捕获的TypeError:无法读取属性'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;
}

1 个答案:

答案 0 :(得分:0)

尝试替换代码

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

以下

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