简体   繁体   English

jquery append 在 IE/Firefox 中不工作

[英]jquery append not working in IE/Firefox

I have the following HTML and subsequent jQuery which appends the related HTML elements after the JSON request has been retrieved.我有以下 HTML 和随后的 jQuery 在检索到 Z0ECD11C1D7A287401D148A23BBD 请求之后附加了相关的 HTML 元素。

This implementation works in Google Chrome + Android browser + Safari, but is not populating the data in Firefox or Internet Explorer 9.此实现适用于 Google Chrome + Android 浏览器 + Safari,但不会填充 Firefox 或 Internet Explorer 9 中的数据。

* * Works in Chrome, Android browser, Firefox 4... does not work in Firefox 3.x and IE. * *适用于 Chrome、Android 浏览器、Firefox 4... 不适用于 Firefox 3.x 和 IE。

Static HTML: Static HTML:

   <header class=line>
                       <div class="unit size3of4">
                            <h2 class="fullname"></h2>
                            <h4 class="nickname"></h4>
                       </div>
   </header>

The jQuery code: jQuery 代码:

<script>
function load_user_info() {
        var content_url = 'rest.api.url.com';
        $.getJSON(content_url, {id:'11xx1122xx11'}, function(data){
            console.log(data);
            $.each(data, function(key, value) {
                if (key == "fullname") {$('.fullname').append(value);}
                else if (key == "nickname") {$('.nickname').append(value);}
            });
         });
    }

 load_user_info();
 </script>

Slightly confused about the behavior between browsers.对浏览器之间的行为有点困惑。 I can guarantee the JSON request is returning two variables: fullname & nickname and have confirmed this我可以保证 JSON 请求返回两个变量:全名和昵称并已确认

In Firefox using the FireBug plugin I see the results of console.log(data) .在使用 FireBug 插件的 Firefox 中,我看到了console.log(data)的结果。
In Chrome I see the results of the console.log(data) and the successful display of the fullname & nickname in the HTML after the JSON request.在 Chrome 中,我看到了console.log(data)的结果,并且在 JSON 请求之后在 HTML 中成功显示了全名和昵称。

Using jQuery 1.6.1 if it helps...如果有帮助,请使用 jQuery 1.6.1...

JSON Output: JSON Output:

 {"fullname":"johnny fartburger", "nickname":"jf"}

I'm slightly perplexed by what you're doing.我对你在做什么有点困惑。 I think the following code:我认为以下代码:

$.each(data, function (key, value) {
    if (key == "fullname") {
        $('.fullname').append(value);
    } else if (key == "nickname") {
        $('.nickname').append(value);
    }
});

could be more easily represented by this:可以更容易地表示为:

$('.fullname').append(data.fullname);
$('.nickname').append(data.nickname);

I don't know if this will solve your problem, but it would certainly be an improvement.我不知道这是否会解决您的问题,但这肯定会有所改进。

The resulting problem was from the actual JSON data not being retrieved in IE.由此产生的问题是由于实际 JSON 数据未在 IE 中检索到。 Hours and hours of search turned up the problem was that XDomainRequest is not natively supported by jQuery, specifically in a $.getJSON request经过数小时的搜索,问题是XDomainRequest本身不支持 XDomainRequest,特别是在$.getJSON请求中

http://bugs.jquery.com/ticket/8283 http://bugs.jquery.com/ticket/8283

In the end, I wrote a try {} catch {} statement that checks to see if $.browser.msie and if it is, do not use $.getJSON to retrieve the results, rather:最后,我写了一个try {} catch {}语句来检查是否$.browser.msie ,如果是,不要使用$.getJSON来检索结果,而是:

if ($.browser.msie) {
 var xdr = new XDomainRequest();
 xdr.open('GET', url);
 xdr.onload = function() {
     var data = $.parseJSON(this.responseText);
     show_data(data);                
 }
 xdr.send();
} else {
 $.getJSON(url, function(data){
     show_data(data);
 });
}

So... conclusion append does work in IE (7/8/9) and Firefox (3/4) but not when the AJAX results aren't being returned.所以...结论append 在 IE (7/8/9) 和 Firefox (3/4) 中确实有效,但在 AJAX 结果未返回时无效。 Thanks everyone for your help and insight.感谢大家的帮助和洞察力。

.append() takes a HTML string as a parameter to append to the existing content. .append()将 HTML 字符串作为参数传递给现有内容的 append。 You probably want to replace the element's text, use .text() or .html() instead, depending on the contents of the value variable.您可能想要替换元素的文本,使用.text().html()代替,具体取决于value变量的内容。

.append(value) is not very safe (for example if value="div"). .append(value)不是很安全(例如,如果 value="div")。
You should use .html(value)你应该使用.html(value)

And what does data contain? data包含什么? I think .append is used to append elements .我认为.append用于 append元素 If data is plain text, that might not work (haven't tried it actually).如果数据是纯文本,那可能不起作用(实际上没有尝试过)。 Chrome uses a different engine and may convert the text to a textnode in the DOM. Chrome 使用不同的引擎,可能会将文本转换为 DOM 中的文本节点。

Use .text() or .html() to set the contents of an element.使用.text().html()设置元素的内容。

as an alternative with appendTo , try to use .text() too作为appendTo的替代方法,也尝试使用.text()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM