jQuery Find()和XML在IE中不起作用 [英] jQuery Find() and XML does not work in IE

查看:74
本文介绍了jQuery Find()和XML在IE中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用jQuery来解析内存中的XML文档。除了IE(震撼)之外,这在一切都很好。一些谷歌搜索显示问题很可能是因为IE将我的文档视为HTML而不是XML MIME类型。

I am trying to use jQuery to parse an in-memory XML document. This works great in everything but IE (shocker). Some googling revealed that the problem is most likely due to the fact that IE is treating my document as HTML rather than XML MIME Type.

有没有办法让我的jQuery implmentation工作,还是我必须检查客户端浏览器并实现IE特定的XML解析?

Is there a way to make my jQuery implmentation work or do I have to check the client browser and implement IE specific XML parsing?

谢谢!!

function getQAData(xmlData) {
var dataArr = new Array();
$(xmlData).find('item').each(function() {
    dataArr.push({ questionid: $(this).attr("cellID"), answer: $(this).find('answer').text() });
});

return dataArr; // this array is nice and populated in everything but ie

XML Sample

XML Sample

    <hwroot>
    <data pid="">
        <item cellID="24_951">
            <question>Are you there?</question>
            <answer>No</answer>
        </item>
        <item cellID="24_957">
            <question>A Question?</question>
            <answer>blah blah blah</answer>
        </item>
</data>
</hwroot>

解决方案:

我的jQuery .find()解决方案不适用于@treeface下面描述的原因。这是我的解决方案:

My jQuery .find() solution will not work for reasons described by @treeface below. Here is my solution:

基于导航器的分支JS代码:(

Branch JS code based on navigator :(

  if (browserName == "Microsoft Internet Explorer") {
    MsXmlParse(xmlData, dataArr);

} else {
    $(xmlData).find('item').each(function() {
        dataArr.push({ questionid: $(this).attr("cellID"), answer: $(this).find('answer').text() });
    });
}

MsXmlParse的定义

Definition of MsXmlParse

function MsXmlParse(xmlDocument, qarray) {
var xmldoc = new ActiveXObject("Microsoft.XMLDOM");
xmldoc.async = "false";
xmldoc.loadXML(xmlDocument);

var itemElements = xmldoc.getElementsByTagName("item");

for (var i = 0; i < itemElements.length; i++) {
    var questionCellID = itemElements[i].getAttributeNode("cellID").nodeValue;
    var answerTxt = itemElements[i].childNodes[1].text;

    qarray.push({ questionid: questionCellID, answer: answerTxt });
}

} //结束msxmlparse

} //end msxmlparse

推荐答案

我对此没有任何问题,你可以在这里看到:

I'm not having any issues with this as you can see here:

http://jsfiddle.net/treeface/VuwcH/

我的猜测是,在IE中,你首先没有正确地解析你的XML数据,但除非你向我们展示那个xmlData变量的输出,否则你无法分辨。正如您在我的示例中所看到的,您需要使用除 DOMParser 对象之外的其他内容,以便在IE中正确解析XML字符串。你必须这样做:

My guess is that in IE, you're not parsing your XML data properly in the first place, but it's impossible to tell unless you show us the output of that xmlData variable. As you can see in my example, you need to use something other than the DOMParser object in order to parse an XML string properly in IE. You have to do this:

xmlData = new ActiveXObject("Microsoft.XMLDOM");
xmlData.async = "false";
xmlData.loadXML(text); //where text is your XML string

编辑

好的,这里的问题是你的 xmlData 对象是一串XML吗?您在帖子中提到它是内存中的XML文档,在这种情况下它会在IE中运行。据我所知,xmlData实际上是一个字符串,而这就是问题出现的地方。 IE是唯一一个在HTML节点中放置不兼容HTML的主要浏览器。由于jQuery的XML解析器将提供的字符串包装在 div 中,因此它在IE中出现了问题。 jQuery认识到这个错误,他们认为这是一个无法修复的问题。

OK, so is the problem here that your xmlData object is a string of XML? You mentioned in your post that it is "an in-memory XML document", in which case it would, and does, work in IE. As far as I can tell from your responses, xmlData is actually a string, and that's where the problems arise. IE is the only major browser that has issues with putting non-compliant HTML inside an HTML node. Since jQuery's XML parser wraps the supplied string in a div, it craps out in IE. jQuery recognizes this bug and they have considered it a "won't fix" issue.

问题在这里: http://bugs.jquery.com/ticket/3143

不修理原因:检查此链接: http://api.jquery.com/jQuery#jQuery2 jQuery构造函数只支持html,而不支持xml。该字符串被刷新为div,这可能是IE爆炸的原因。因为字符串无效html。

Reason for not fixing: Check this link : http://api.jquery.com/jQuery#jQuery2 The jQuery constructor only supports html, not xml. The string is flushed into a div, that probably why IE blows up. Because the string isn't valid html.

特别是这一点:


当HTML比没有属性的单个标签更复杂时,如上例所示,元素的实际创建由浏览器的 innerHTML 处理机制。在大多数情况下,jQuery会创建一个新的< div> 元素,并将该元素的 innerHTML 属性设置为HTML传入的代码段。当参数包含单个代码时,例如 $('< img />') $(' < a>< / a>'),jQuery使用原生JavaScript createElement()函数创建元素。

When the HTML is more complex than a single tag without attributes, as it is in the above example, the actual creation of the elements is handled by the browser's innerHTML mechanism. In most cases, jQuery creates a new <div> element and sets the innerHTML property of the element to the HTML snippet that was passed in. When the parameter has a single tag, such as $('<img/>') or $('<a></a>'), jQuery creates the element using the native JavaScript createElement() function.

传递复杂的HTML时,某些浏览器可能无法生成完全复制所提供的HTML源的DOM。例如,版本8之前的Internet Explorer会将链接上的所有href属性转换为绝对URL,而版本9之前的Internet Explorer将无法正确处理HTML5元素,而无需添加单独的兼容层。

When passing in complex HTML, some browsers may not generate a DOM that exactly replicates the HTML source provided. For example, Internet Explorer prior to version 8 will convert all href properties on links to absolute URLs, and Internet Explorer prior to version 9 will not correctly handle HTML5 elements without the addition of a separate compatibility layer.

所以我怀疑他们不会修复它,因为否则所有其他浏览器都会变得松散。我不确定这个答案的真实性(或者他们决定背后的逻辑质量)但是就是这样。

So I suspect that they "won't fix" it because otherwise things would get sloppy for all other browsers. I'm not sure of the veracity of this answer (or the quality of the logic behind their decision) but that's the way it is.

所以...简而言之(太迟了?)...不,没有简单的方法可以在一行中处理这个问题。再说一遍,也不应该阻止你使用JSON而不是XML。

So...in short (too late?)...no, there is no easy way to handle this in a single line. Then again, there also shouldn't be anything stopping you from using JSON instead of XML.

这篇关于jQuery Find()和XML在IE中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆