【问题标题】:InvalidCharacterError with appendChild on onload in IE only仅在 IE 中加载时带有 appendChild 的 InvalidCharacterError
【发布时间】:2015-04-20 14:02:45
【问题描述】:

我正在尝试在 IE11 中执行 Javascript 函数,但在 appendChild 上出现 InvalidCharacterError。

function fixWidth(){
   var elems=document.getElementsByTagName("*");
   for(e=0; e<elems.length; e++){
      var eCurStyle = elems[e].currentStyle;
      var l_minWidth = (eCurStyle.minWidth) ? eCurStyle.minWidth : eCurStyle.getAttribute("min-width"); 
      if(l_minWidth && l_minWidth != 'auto'){
         var shim = document.createElement("DIV");
         shim.style.cssText = 'margin:0 !important; padding:0 !important; border:0 !important; line-height:0 !important; height:0 !important; BACKGROUND:RED;';
         shim.style.width = l_minWidth;
         shim.appendChild(document.createElement("&nbsp;"));
         elems[e].appendChild(shim);
      }
   }
}
window.onload = fixWidth;

它可以在 IE8 上正常运行。但不是在 IE11 上。

【问题讨论】:

    标签: javascript html internet-explorer onload appendchild


    【解决方案1】:

    这是 IE8 中的错误,而不是 IE11 中的错误

    document.createElement("&nbsp;")
    

    不应该在任何地方真正起作用,因为不间断的空间不是一个元素。
    Chrome 报错

    未捕获的 InvalidCharacterError:无法执行“createElement” “元素”:提供的标签名称 ('&amp;nbsp;') 不是有效名称。

    我怀疑几乎所有浏览器 IE8 会抛出错误

    如果你想给元素添加一个不间断的空格,你可以这样做

    shim.innerHTML = shim.innerHTML + "&nbsp;";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-15
      • 2012-03-07
      • 1970-01-01
      • 2011-10-28
      • 2015-07-11
      相关资源
      最近更新 更多