Cannot read property 'backgroundColor' of undefined

孤街浪徒 提交于 2019-12-11 11:54:19

问题


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;
}

回答1:


Try to replace the code

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

with the following

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


来源:https://stackoverflow.com/questions/36244757/cannot-read-property-backgroundcolor-of-undefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!