【问题标题】:jQuery $(element).html() not working in ie7jQuery $(element).html() 在 ie7 中不起作用
【发布时间】: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


【解决方案1】:

首先检查元素是否存在,

如果您确认相同,请确保数据不为空

如果一切正常

那就试试$('#parent_div',"container").html(response);

这会解决你的问题

【讨论】:

    猜你喜欢
    • 2010-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多