Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript document.write()和document.images[i].width在IE8中失败!_Javascript_Internet Explorer 8_Html - Fatal编程技术网

Javascript document.write()和document.images[i].width在IE8中失败!

Javascript document.write()和document.images[i].width在IE8中失败!,javascript,internet-explorer-8,html,Javascript,Internet Explorer 8,Html,我正在使用document.write()调试一些代码,并在IE8中发现了一些非常奇怪的行为。是我还是这只虫子 只有4个img元素的页面: <img src="img/one.jpg" width="100" height="75" alt="1st Demo Image" title=""> <img src="img/two.jpg" width="100" height="75" alt="2nd Demo Image" title=""> <img src=

我正在使用
document.write()
调试一些代码,并在IE8中发现了一些非常奇怪的行为。是我还是这只虫子

只有4个
img
元素的页面:

<img src="img/one.jpg" width="100" height="75" alt="1st Demo Image" title="">
<img src="img/two.jpg" width="100" height="75" alt="2nd Demo Image" title="">
<img src="img/three.jpg" width="100" height="75" alt="3rd Demo Image" title="">
<img src="img/four.jpg" width="100" height="75" alt="4th Demo Image" title="">
<img src="img/one.jpg" width="100" height="75" alt="1st Demo Image">
<img src="img/two.jpg" width="100" height="75" alt="2nd Demo Image">
<img src="img/three.jpg" width="100" height="75" alt="3rd Demo Image">
<img src="img/four.jpg" width="100" height="75" alt="4th Demo Image">
<pre><script type="text/javascript">
// Only the first line is output in IE8, other browsers OK
for (var i=0; i<document.images.length; i++) {
    document.writeln(i + ' - ' + document.images[i]['width']);
}
</script></pre>
除IE8外,所有浏览器都按预期输出4行。然而,IE8只输出第一行,用于第一个图像!为什么

循环“循环”正常。您可以使用
alert()
输出
width
属性,并在IE8中获得4个警报,但仍然使用
document.write()
仅输出第一行

但是,如果在打开
标记后只放一个空格,问题就会消失(但输出中会有一个额外的空格)


而且,这似乎只会影响
宽度
高度
(数字?)属性

可能是您的
文档。write
清除文档(我不确定这是否符合规范或实现要求,但它将调用
document.open()
,它将清除包含图像的文档)。因此,在第一个
document.write()
调用清除文档后,不再有图像,因此当循环继续访问
document.images[1]
时,该项不再可用


这解释了为什么任何其他方法都可以工作,因为警告或连接字符串不会清除文档。

如果文档在加载文档后运行,文档将覆盖正文的内容

试试这个:

var htm=[];

对于(var i=0;iI确实对此感到奇怪,但是
document.write()
不在事件处理程序中,它在文档完全写入之前就在页面的主体中。输出其他属性可以正常工作。使用
document.write()输出
警报()
确实会产生4个警报,如果第一次写入()是否已清除文档?@w3d,如果文档尚未编写,则您不应尝试访问其内部内容…这是出现在关闭
正文
标记之前的测试/调试代码,因此DOM应该足够完整,该代码可以正常工作。事实上,这似乎根本不是DOM问题。我已更新了问题(编辑#2)一个确切的场景似乎触发了这一现象。这似乎只会影响IE8。是的,很抱歉,我在最初的问题中忽略了这一点。代码不是在事件中运行的。它是在解析文档时在页面中运行的。正如我在问题中提到的,如果我在循环中串联一个字符串并在使用
document.write()
进行e循环,然后它也可以工作,但我喜欢数组的想法-谢谢。
for (var i=0; i<document.images.length; i++) {
    document.writeln(i + ' - ' + document.images[i]['width']);
    alert(i + ' - ' + document.images[i]['width']);    // Alerts 4 times
}
<img src="img/one.jpg" width="100" height="75" alt="1st Demo Image">
<img src="img/two.jpg" width="100" height="75" alt="2nd Demo Image">
<img src="img/three.jpg" width="100" height="75" alt="3rd Demo Image">
<img src="img/four.jpg" width="100" height="75" alt="4th Demo Image">
<pre><script type="text/javascript">
// Only the first line is output in IE8, other browsers OK
for (var i=0; i<document.images.length; i++) {
    document.writeln(i + ' - ' + document.images[i]['width']);
}
</script></pre>
var htm = [];
for (var i=0; i<document.images.length; i++) {
    htm.push(i + ' - ' + document.images[i]['width']); 
}

document.write(htm.join(''));