“的innerHTML”:对象为null或undefined [英] 'innerHTML': object is null or undefined

查看:1029
本文介绍了“的innerHTML”:对象为null或undefined的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下内容:

<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?

的HTML是使用被附加表变种也由JavaScript在for循环 + =

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

推荐答案

这是因为你执行你的code满载DOM之前。

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

此的的工作:

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

或者你可以简单地把你的JavaScript code之前刚&LT; / BODY&GT; ,无需使用标记的onload 那里。

这篇关于“的innerHTML”:对象为null或undefined的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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