无法读取未定义的属性“backgroundColor" [英] Cannot read property 'backgroundColor' of undefined

查看:82
本文介绍了无法读取未定义的属性“backgroundColor"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When the function call below gets run it returns this error. "Uncaught TypeError: Cannot read property 'backgroundColor' of undefined" I am trying to change the background color of a class called .jumbotron. I have tried everything I can thing of so far.

Could anyone tell me why this is happening?

clrElementJumbo("cornsilk");

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

推荐答案

尝试替换代码

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

以下内容

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

这篇关于无法读取未定义的属性“backgroundColor"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆