【发布时间】:2011-05-11 09:43:08
【问题描述】:
我在使用 IE7 时遇到了一个奇怪的问题。我正在使用 ajax 请求在 div 元素中加载内容。在 ajax 响应 html 中有一个 id 为“compare_div”的 div,其中包含一些内容。 当我尝试使用 $('#compare_div').html() 获取 html 时,它返回 null。
下面是我的javascript代码,
function myFunction()
{
$.ajax({
type : "post",
data: data,
url : my_url,
success : function(response) {
$('#parent_div').html(response);
var compare_div_html = $('#compare_div').html();
},
error : function(XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
}
});
return false;
}
我在 compare_div_html 变量中得到空值。此代码适用于所有其他浏览器。
请帮忙。
【问题讨论】:
-
“#compare_div”应该是什么?因为您正在用 Ajax 响应填充“#parent_div”。
-
试试
alert($('#compare_div').length)看看在IE7中是否真的存在compare_div。 -
如果您有另一个具有相同 id 的元素尝试
alert($('[id=compare_div]').length);并确保只有 id=compare_div 的元素,则可能会发生这种情况 -
我的 ajax 响应类似于......
-
@Gary Green, $('#compare_div').length 返回 0,所以我认为响应没有正确附加到父 div,但它只发生在 IE7 上,它适用于 IE8 和 IE9
标签: jquery internet-explorer-7