开发者

problem with jQuery's .find().length in Internet Explorer 8 [closed]

开发者 https://www.devze.com 2023-02-09 09:12 出处:网络
开发者_如何转开发 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time,or an extraordinarily narrow situation that is not
开发者_如何转开发 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 10 years ago.

I've Googled around, but I can't quite find a good solution to this problem. IE's developer tools haven't helped much on this front either.

This code for an xml document returned via ajax works in Safari, Chrome and Firefox:

$(data).find('Ticket').length;

The code returns, for example, the number 3.

The same code returns 0 in Internet Explorer 8. Why?

I have no doubt there's a simple explanation that involves either a) an IE quirk or b) a fault in my code that the other browsers forgive.


find doesnt work in IE with custom tags (xml) unless you construct an activeX object

assuming the variable 'xml' is what is the xml returned from your request

    var data;

    if ($.browser.msie) 
    {
       data = new ActiveXObject("Microsoft.XMLDOM");
       data.async = false;
       data.loadXML(xml);
    } 
    else 
    {
       data = xml;
    }
    //your code here


Edit: Next answer.

Try

$('Ticket', data).length;

IE grabs xml differently than other browsers, firefox and chrome, alert($data) see if it is empty


Try out $(data).find('ticket').size(). This returns the same result as of $(data).find('ticket').length


Firstly, find() does in fact work in IE8, although perhaps it didn't back in February 2011.

In any case, I had exactly this problem today, and for me it was caused by the original ajax request: I had dataType: 'html' instead of dataType: 'xml'.

It's a silly mistake, but worth checking for.

0

精彩评论

暂无评论...
验证码 换一张
取 消