某些时候用IE打开页面时出现如下的错误警告窗口:
—————————
Windows Internet Explorer
—————————
Internet Explorer 无法打开 Internet 站点http://www.junstyle.com.cn。
已中止操作
—————————
确定
—————————
当点击确定页面就无法打开了,出现了一个错误的页面。这可能是因为你的javascript脚本在页面还没有载入完毕的时候改变了dom的结构,也就是用appendChild()向body里面添加了内容(子元素),导致body的结构错误,IE就无法显示网页了,当然在firefox中不会出现这个错误,一切正常。在IE中错误的用了如下的方法:
<!DOCTYPE html public "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<body>
<div>
<script type="text/javascript">
document.body.appendChild(document.createElement("div"));
</script>
</div>
</body>
</html>
你可以用如下代码代替这个添加div的操作:
document.body.insertBefore(document.createElement(&quot;div&quot;), document.body.childNodes[0]);
使用insertBefore方法就不会出现前面的那个错误。