问题
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