Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
434 views
in Technique[技术] by (71.8m points)

javascript - 'innerHTML': object is null or undefined

I have the following:

<div id="valueboxes" style="overflow-x:hidden;overflow-y:scroll;width:100%;height:200px">

However, every time I use the following:

document.getElementById("valueboxes").innerHTML = html;

I get this error:

'innerHTML': object is null or undefined

Am I doing something wrong?

The html is a table var being appended too by javascript in a for loop using +=.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

That's because you are executing your code before DOM is fully loaded.

This should work:

window.onload = function() {
  document.getElementById('valueboxes').innerHTML = html;
};

Or you can simply put your javascript code just before </body> tag with no need to use onload there.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...