javascript高级程序设计讲到IE中的错误时提到操作中止(operation shorted)这个概念:
在修改尚未加载完成的页面会发生错误。
示例代码为:
<body> <p></p> <div> <script> document.body.appendChild(document.createElement("div")); </script> </div></body>
当<script>被包含在某元素中,且JS代码要使用DOM方法修改其父元素或祖先元素,会发生操作中止错误(因为只能修改已经加载完的元素)。
而改为
document.body.insertChild(document.createElement("div"),document.body.firstChild);
就能避免错误。
说是新<div>添加到document.body开头而不是末尾,就没有错误。
慕娘9325324
相关分类