【问题标题】:jquery .html() does not work on ie8jquery .html() 在 ie8 上不起作用
【发布时间】:2011-03-07 13:32:28
【问题描述】:

我有一个 jquery 函数,它对 web 服务器上的 webservice 方法进行 ajax 调用,该方法返回一个带有数据的 html 表。我正在使用 .html() 在 div 上呈现返回值。这适用于 Firefox、Chrome、Safari,但不适用于 IE8

$.ajax({
    type: "POST",
    url: "./../WebAjaxCalls.asmx/GetProductInstruction",
    data: "{'ProductID':'" + $("#txtProductID").val()  + "'}",
    success: function(data) {
        if (data.d[0] == "true") {
            **$("#dvProudctInstruction").html(data.d[1]);** 
        }
    },
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    error: function(e, textStatus, errorThrown) {
        bReturn = false;
    }
});  

$("#dvProudctInstruction").html(data.d[1]); 行适用于除 IE8 之外的所有浏览器。

对此的任何帮助将不胜感激。

【问题讨论】:

    标签: jquery jquery-selectors


    【解决方案1】:

    您可以在分配 html() 之前提醒您的响应

    你可以使用 innerHTML 属性来代替 jquery 的 html() (虽然它是一样的)

    您可能想查看this 链接

    【讨论】:

    • 链接+1。在我的情况下解决了 IE8 的问题(问题变成了错误的标记!)。它说:在 IE 中,jQuery .html() 属性不会替换第一个匹配元素的内容,除非新的 HTML 有效
    【解决方案2】:

    似乎 IE8 在使用 jQuery 的 html() 插入长字符串时出现问题,导致 div 的内容完全空白。 刚刚尝试了一个很长的字符串和一个只包含“blah”的字符串,结果完全不同。

    因此,如果您期待大量文本(例如 2k+ 个字符),请选择原生的 innerHTML。 没有做任何进一步的研究,所以我不知道在 IE8 中通过 html() 传递的字符串的最大长度是多少。

    【讨论】:

      【解决方案3】:

      您是否尝试过将data.d[1] 设置为变量然后添加它?

      例如:

      if (data.d[0] == "true") {
           var results = data.d[1];
           $("#dvProudctInstruction").html(results);
      }
      

      【讨论】:

        【解决方案4】:

        无论是它的 .html() 不起作用还是其他原因,请先检查它。发出警报,看看您是否得到正确的、预期的 html。

        alert(data.d[1]);
        $("#dvProudctInstruction").html(data.d[1]);
        

        很可能来自 data.d[1] 的 html 包含错误并且 IE 无法解决它。

        【讨论】:

          【解决方案5】:

          只是一种直觉,但你确定你进入了 if 块吗?我只是问我自己曾几次偶然发现“真实”与“真实”……

          试试:

          success: function(data) {
              alert('outside of if');
              if (data.d[0] == "true") {
                  alert(data.d[1]);
                  $("#dvProudctInstruction").html(data.d[1]);
              }
          }
          

          可能没什么,但至少你会消除你根本不会运行你突出显示的代码行的可能性。

          【讨论】:

            【解决方案6】:

            我已经在link 上回答了这个问题 你只需要改变你的代码是这样的

            $("#dvProudctInstruction").html(data.d[1].toString());
            

            【讨论】:

              【解决方案7】:

              我知道答案有点晚了;但您也可以使用 try/catch 块来修复它。有时您可能无法完全控制内容。

              这样,您可以让原始代码在新浏览器上运行,而 catch 运行 IE8 的 innerHTML。

              try {
                  //new browsers
                  $('#dvProudctInstruction').html(data.d[1]);
              } catch (e) {
                  //IE8
                  $('#dvProudctInstruction').innerHTML = data.d[1];
              }
              

              希望这对某人有所帮助!

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2011-09-03
                • 2013-06-18
                • 2012-09-12
                • 2012-07-08
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多