为什么IE设置innerHTML时,会出现意想不到的错误(Why does IE give unex

2019-07-22 06:49发布

我试图设置的innerHTML在Firefox中的元素,并将其与没有明显的原因,工作正常,尝试在IE中,得到了意外的错误。

例如,如果你尝试并设置表的innerHTML为“喜从STU”它会失败,因为该表必须跟一个序列。

Answer 1:

“显然,Firefox是不是这个挑剔” ==>显然Firefox是如此越野车,它不注册这个明显违反了基本的HTML嵌套规则...

正如有人在另一个论坛上指出,Firefox会接受,你追加一个完整的HTML文档作为一个表单域的孩子,甚至图像, - (



Answer 2:

你看到这种行为,因为innerHTML的是只读的在IE表元素。 从MSDN的innerHTML属性文件:

该属性为读/写的除了下面,它是只读的所有对象:COL,COLGROUP,FRAMESET,HEAD,HTML,样式,TABLE,TBODY,TFOOT,THEAD,TITLE,TR。



Answer 3:

不知道为什么你被向下改装成的问题斯图,因为这是我最近解决了相当。 诀窍是“唧” HTML到当前未附加到文件树中的DOM元素。 下面的代码片段,做的:

// removing the scripts to avoid any 'Permission Denied' errors in IE
var cleaned = html.replace(/<script(.|\s)*?\/script>/g, "");

// IE is stricter on malformed HTML injecting direct into DOM. By injecting into 
// an element that's not yet part of DOM it's more lenient and will clean it up.
if (jQuery.browser.msie)
{
    var tempElement = document.createElement("DIV");
    tempElement.innerHTML = cleaned;
    cleaned = tempElement.innerHTML;
    tempElement = null;
}
// now 'cleaned' is ready to use...

请注意,我们只使用在这个片段中使用jQuery这里来测试浏览器是否为IE浏览器,有jQuery的没有硬性的依赖性。



Answer 4:

检查您要设置的innerHTML元素的范围。 因为FF和IE处理这个以不同的方式



Answer 5:

http://www.ericvasilik.com/2006/07/code-karma.html



Answer 6:

我一直缠斗与链接不同的表替换表的链接列表的问题。 如上所述,问题自带的IE和表元素的其只读属性。

追加对我来说是不是一种选择,所以我(终于)计算出的(这适用于CH,FF和IE 8.0(没有尝试其他的 - 但我希望))。

replaceInReadOnly(document.getElementById("links"), "<a href>........etc</a>");

function replaceInReadOnly(element, content){
    var newNode = document.createElement();
    newNode.innerHTML = content;
    var oldNode = element.firstChild;
    var output = element.replaceChild(newNode, oldNode);
}

对我的作品 - 我希望它为你的作品



Answer 7:

你有没有尝试设置的innerText和/或的textContent? 一些节点(如SCRIPT标签)将不会像预期的那样,当你试图改变自己在IE的innerHTML。 这里更多关于的innerText与的textContent:

http://blog.coderlab.us/2006/04/18/the-textcontent-and-innertext-properties/



Answer 8:

你要为一个完全不同的innerHTML或innerHTML的替代模式? 我问,因为如果你试图做一个简单的搜索/通过的innerHTML的“权力”取代,你会发现某些类型的元素在IE不打。

这可以通过你的周围在一个try / catch尝试,并通过parentNode冒泡的DOM,直到你成功地管理这样做谨慎纠正。

但是,这不会,如果你插入全新的内容是合适的。



Answer 9:

您可以修改的行为。 下面是一些代码,防止IE否则引用的元素的垃圾收集:

if (/(msie|trident)/i.test(navigator.userAgent)) {
 var innerhtml_get = Object.getOwnPropertyDescriptor(HTMLElement.prototype, "innerHTML").get
 var innerhtml_set = Object.getOwnPropertyDescriptor(HTMLElement.prototype, "innerHTML").set
 Object.defineProperty(HTMLElement.prototype, "innerHTML", {
  get: function () {return innerhtml_get.call (this)},
  set: function(new_html) {
   var childNodes = this.childNodes
   for (var curlen = childNodes.length, i = curlen; i > 0; i--) {
    this.removeChild (childNodes[0])
   }
   innerhtml_set.call (this, new_html)
  }
 })
}

var mydiv = document.createElement ('div')
mydiv.innerHTML = "test"
document.body.appendChild (mydiv)

document.body.innerHTML = ""
console.log (mydiv.innerHTML)

http://jsfiddle.net/DLLbc/9/



Answer 10:

我想通了,如果你尝试设置innerHTML的IE浏览器中的一个元素是没有逻辑改正它会抛出这个错误。 例如,如果你尝试并设置表的innerHTML为“ 喜从STU”它会失败,因为该表必须跟一个序列。 显然,Firefox是不是这个挑剔。 希望能帮助到你。



文章来源: Why does IE give unexpected errors when setting innerHTML