IE innerHTML错误 [英] IE innerHTML error

查看:88
本文介绍了IE innerHTML错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题与已经有关于这个主题的问题有些不同。我使用这个建议来转换一个这样的函数:

This is a little different than the questions that have already been asked on this topic. I used that advice to turn a function like this:

function foo() {

    document.getElementById('doc1').innerHTML = '<td>new data</td>';

}

到这个:

function foo() {

    newdiv = document.createElement('div');
    newdiv.innerHTML = '<td>new data</td>';

    current_doc = document.getElementById('doc1');
    current_doc.appendChild(newdiv);

}

但这仍然不起作用。在这两种情况下,在包含innerHTML的行上都会出现未知的运行时错误。

But this still doesn't work. An "unknown runtime error" occurs on the line containing innerHTML in both cases.

我以为创建newdiv元素并使用innerHTML可以解决问题? >

I thought creating the newdiv element and using innerHTML on that would solve the problem?

推荐答案

不可能在Internet Explorer中单独创建td或tr。在其他浏览器中也存在相同的问题相当一段时间,但是最新版本的这些问题不再受此影响。

It is not possible to create td or tr separately in Internet Explorer. This same problem has existed in other browsers for quite some time too, however latest versions of those do not suffer from that issue any more.

您有两个选项可以: / p>

You have 2 options to:


  1. 使用表格特定的API来添加
    个单元格/行。参见例如用于insertCell的MSDN
    和更多

  2. 创建一个实用程序函数,
    将帮助您创建DOM节点
    的字符串。在表格的情况下,您需要包装HTML,以便生成的HTML始终是一个表,然后通过标签名称获取必需的元素。

  1. Use table specific APIs to add cells/rows. See for example MSDN for insertCell and more
  2. Create a utility function, that would help you creating DOM nodes out of strings. In case of a table you would need to wrap up your HTML so that the resulting HTML is always a table and then get required element by tag name.

例如:

var oHTMLFactory = document.createElement("span");
function createDOMElementFromHTML(sHtml) {
    switch (sHtml.match(/^<(\w+)/)) {
        case "td":
        case "th":
            sHtml   = '<tr>' + sHtml + '</tr>';
            // no break intentionally left here
        case "tr":
            sHtml   = '<tbody>' + sHtml + '</tbody>';
            // no break intentionally left here
        case "tbody":
        case "tfoot":
        case "thead":
            sHtml   = '<table>' + sHtml + '</table>';
            break;
        case "option":
            sHtml   = '<select>' + sHtml + '</select>';
    }
    oHTMLFactory.innerHTML = sHtml;

    return oAML_oHTMLFactory.getElementsByTagName(cRegExp.$1)[0] || null;
}

希望这有帮助!

这篇关于IE innerHTML错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆