IE 错误:无法设置属性“innerHTML”的值:对象为 null 或未定义
这段简单的代码(进度条)在除 IE 之外的任何地方都可以正常工作(尝试过 9 和 8):
<!-- Progress bar holder -->
<div id="progress" style="width:500px;border:1px solid #eee;"></div>
<!-- Progress information -->
<div id="information" style="width"></div>
<?php
// Total processes
$total = 10;
// Loop through process
for($i=1; $i<=$total; $i++){
// Calculate the percentation
$percent = intval($i/$total * 100)."%";
// Javascript for updating the progress bar and information
echo '<script language="javascript">
document.getElementById("progress").innerHTML="<div style=\"width:'.$percent.'; background-color:#ddd;\"> </div>";
document.getElementById("information").innerHTML="'.$i.' row(s) processed.";
</script>';
// This is for the buffer achieve the minimum size in order to flush data
echo str_repeat(' ',1024*64);
// Send output to browser immediately
flush();
// Sleep one second so we can see the delay
sleep(1);
}
// Tell user that the process is completed
echo '<script language="javascript">document.getElementById("information").innerHTML="Process completed"</script>';
?>
IE 显示错误“无法设置属性‘innerHTML’的值:对象为 null 或未定义”。 问题似乎出在这里:
document.getElementById("progress").innerHTML="<div style=\"width:'.$percent.';background-color:#ddd;\"> </div>";
这种情况下的 div 在 IE 中无法正常工作(至少据我所知)
尝试自己修复它,但这对我来说太复杂了。任何帮助将不胜感激。 谢谢 )
This simple piece of code (progress bar) works fine everywhere except IE (tried 9 and 8) :
<!-- Progress bar holder -->
<div id="progress" style="width:500px;border:1px solid #eee;"></div>
<!-- Progress information -->
<div id="information" style="width"></div>
<?php
// Total processes
$total = 10;
// Loop through process
for($i=1; $i<=$total; $i++){
// Calculate the percentation
$percent = intval($i/$total * 100)."%";
// Javascript for updating the progress bar and information
echo '<script language="javascript">
document.getElementById("progress").innerHTML="<div style=\"width:'.$percent.'; background-color:#ddd;\"> </div>";
document.getElementById("information").innerHTML="'.$i.' row(s) processed.";
</script>';
// This is for the buffer achieve the minimum size in order to flush data
echo str_repeat(' ',1024*64);
// Send output to browser immediately
flush();
// Sleep one second so we can see the delay
sleep(1);
}
// Tell user that the process is completed
echo '<script language="javascript">document.getElementById("information").innerHTML="Process completed"</script>';
?>
IE shows the error " Unable to set value of the property 'innerHTML': object is null or undefined ".
The problem seems to be here :
document.getElementById("progress").innerHTML="<div style=\"width:'.$percent.';background-color:#ddd;\"> </div>";
div in this case doesn't work properly in IE (at least as far as I understood)
Tried to fix it by myself, but it's too complicated for me. Any help will be much appreciated.
Thanks )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该文档可能尚未加载。
尝试一下
,否则我总是使用 jQuery
the document is probably not loaded yet.
try
or I always use jQuery
该解决方案最有可能确保您的文档具有正确的结构。
这不起作用:
这起作用:
The solution is most likely to ensure your document has the correct structure.
This does not work :
This does work: