IE KB927917 (Unable to modify the parent container element before the child element is closed )

错误提示:Unable to modify the parent container element before the child element is closed (KB927917) 
简单描述:子HTML元素内包含试图修改子容器的父容器元素的脚本。 
页面截图:ie6


 

ie8



这个问题是伟大的浏览器IE发现的,各大浏览器没有这种反馈,原因就是解析的JavaScript的机制不同。因为IE解析JavaScript是边读边执行的,所以如果脚本允许修改父元素,并且子元素又没关闭的话,会失去条理性。 这个问题一直到IE8还是存在。

简单的解决方法就是在script标签上加上 defer="true"
<script type="text/javascript" src="..."></script>
改成
<script type="text/javascript" src="..." defer="true"></script>

 

详细解释:http://support.microsoft.com/kb/927917 
MSDN地址:http://blogs.msdn.com/ie/archive/2008/04/23/what-happened-to-operation-aborted.aspx 

官方给出的解决办法如下: 
   1. Moving your script execution to a function that is invoked after parsing is complete (e.g., onload) 
   2. Adding the defer boolean attribute to the script block (this defers execution of the script content until parsing is complete) 
   3. Limiting your tree modifications to the script-element's immediate parent 
   4. Moving the location of your script block to a child of the body (this usually solves most problems, while allowing the most flexibility in terms of scenarios).

 

常规js解决办法

其实很简单,就是把js代码放到需要修改的父元素外就可以解决了。
下面是微软给出的解释。
http://support.microsoft.com/kb/927917
出现此问题的原因子容器 HTML 元素包含试图修改子容器的父容器元素的脚本。 脚本试图使用 innerHTML 方法或 appendChild 方法修改父容器元素。

是例如如果 DIV 元素是在 BODY 元素中的子容器,并在 DIV 元素中的一个 SCRIPT 块尝试修改 DIV 元素的父容器的 BODY 元素,可能会出现此问题。
示例 1
在本例中, DIV 元素是一个子容器元素。 SCRIPT 块在 DIV 元素内的尝试修改 BODY 元素。 BODY 元素是关闭的父容器的 DIV 元素。
<html>
  <body>
      <div>
                  <script type="text/Javascript">
                    document.body.innerHTML+="sample text";
                  </script>
      </div>
  </body>
</html>要解决此问题,请使用以下方法之一。
方法 1: 修改父元素
将 SCRIPT 块移动到 BODY 元素的范围。 这是脚本试图修改的容器。
<html>
  <body>
      <div>
      </div>
      <script type="text/Javascript">
           document.body.innerHTML+="sample text";
      </script>
  </body>
</html>
方法 2: 修改一个已关闭的容器元素
作为在父容器元素中的占位符中添加一个已关闭的容器。 然后,修改与脚本块新的已关闭的容器。
<html>
  <body>
      <div id="targetContainer">
      </div>
      <div>
      <script type="text/Javascript">
           document.getElementById('targetContainer').innerHTML+="sample text";
      </script>
      </div>
  </body>
</html>


 

引用了jquery库时解决办法

在js外面用$(document).ready(function(){

$("body").html("text");

})

这样就可以了,表示在页面加载完成后再执行该JS,这样就是在父元素创建完成后再执行js,不会出现这个错误了。

转载于:https://www.cnblogs.com/terryyu/archive/2011/12/14/2287405.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值