【问题标题】:Setting innerHTML with JQuerys html() method in IE 8 not working在 IE 8 中使用 JQuerys html() 方法设置 innerHTML 不起作用
【发布时间】:2011-12-15 16:20:01
【问题描述】:

我有

$('#structure').html(html);

这在 FireFox 中运行良好。 IE 8 不显示 html。我还尝试了简单的文本,例如。

$('#structure').html("Test");

$('#structure').html("<h3>Test</h3>");

All 不能在 IE 8 中工作,但在 Firefox 中。所以我不认为这是无效html的问题。

如果我用

替换它
document.write(html);

IE 正确显示内容。

我迷路了。我还尝试了 getElementById 并设置 innerHTML 属性或 .empty 并附加。基本上我尝试的所有东西都在 FF 中工作,在 IE 8 中没有。

代码在 .change() 事件中运行,该事件也在 IE 中正确执行。

有什么想法吗?

编辑:

html 由第三方商业插件返回。它返回用于显示 ActiveX 组件 (IE) 或插件 (FF) 的浏览器特定代码。返回的 html 有效。这不是我唯一使用它的地方。

也许您不能异步加载 ActiveX?这可以解释一切。

$(document).ready(function() {

    $('#ID').change(function() {            
        var value = $(this).val();
        getStructure(value);
    });
});


function getStructure(id){          
    $.get(
        'getStructure.php?id=' + id,
        function(data){     
            // 3rd party function call:
            var html = getObjectTag(182, 172, data.Structure); // data["Structure"] does not work too in IE, it does in FF
            // alert(html); // is displayed also in IE when uncommented
            $('#structure').html(html); 
            // alert(html); // is displayed also in IE when uncommented             
    }, 
    "json"
    );          
}
<div class="cssform">
    <div><label>ID</label><input id="ID" type="text"/></div>
</div
<div id="structure"></div>

请注意,我在另一个应用程序中使用:

var html = getObjectTag(182, 172, aData[0]);
$('td:eq(0)', nRow).html(html);

其中 aData 是调用函数的数组参数。这用于在 jquery 插件数据表中显示对象,这里 ti 适用于 IE 8 和 FF,没有任何问题。

EDIT2:

进一步的“分析”让我相信,无论出于何种原因,在 IE 中都没有正确找到 id=structure 的 div。

document.getElementById("structure").innerHTML = html;

导致错误“document.getElementById(...)”为空或不是对象。使用 jquery 选择器可能不返回任何内容,因此不会做任何进一步的事情,也不会发生错误。问题是,为什么找不到元素?对我来说毫无意义。

【问题讨论】:

  • 如果您发布一个指向该页面的链接,或者在 jsfiddle.com 上发布您的代码,这样我们就可以查看正在发生的所有事情,这会有所帮助。 $('#structure').html('test');应该适用于所有浏览器。
  • 你的错误一定在别处,你贴的代码是有效的。

标签: jquery internet-explorer-8 innerhtml


【解决方案1】:

好的。找到答案了:

<div class="cssform">
    <div><label>ID</label><input id="ID" type="text"/></div>
</div <!-- Missing closing > which somehow was accepted by FF -->
<div id="structure"></div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多