jQuery .find()不会在IE中返回数据,但会在Firefox和Chrome中返回 [英] jQuery .find() doesn't return data in IE but does in Firefox and Chrome

查看:139
本文介绍了jQuery .find()不会在IE中返回数据,但会在Firefox和Chrome中返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过为他做一些网络工作,我帮助了一位朋友。他需要的部分内容是在他的网站上更改几段文字的简单方法。我没有让他编辑HTML,而是决定提供一个带有消息的XML文件,我使用jQuery将它们从文件中拉出来并将它们插入到页面中。

I helped a friend out by doing a little web work for him. Part of what he needed was an easy way to change a couple pieces of text on his site. Rather than having him edit the HTML I decided to provide an XML file with the messages in it and I used jQuery to pull them out of the file and insert them into the page.

效果很好......在Firefox和Chrome中,在IE7中效果不是很好。我希望你们中的一个能告诉我原因。我做了一个公平但谷歌搜索,但找不到我想要的。

It works great... In Firefox and Chrome, not so great in IE7. I was hoping one of you could tell me why. I did a fair but of googling but couldn't find what I'm looking for.

这是XML:

<?xml version="1.0" encoding="utf-8" ?>
<messages>
  <message type="HeaderMessage">
    This message is put up in the header area.
  </message>
  <message type="FooterMessage">
    This message is put in the lower left cell.
  </message>
</messages>

这是我的jQuery调用:

And here's my jQuery call:

<script type="text/javascript">
  $(document).ready(function() {
    $.get('messages.xml', function(d) {
      //I have confirmed that it gets to here in IE
      //and it has the xml loaded.
      //alert(d); gives me a message box with the xml text in it
      //alert($(d).find('message')); gives me "[object Object]"
      //alert($(d).find('message')[0]); gives me "undefined"
      //alert($(d).find('message').Length); gives me "undefined"
      $(d).find('message').each(function() {
        //But it never gets to here in IE
        var $msg = $(this);
        var type = $msg.attr("type");
        var message = $msg.text();
        switch (type) {
        case "HeaderMessage":
          $("#HeaderMessageDiv").html(message);
          break;
        case "FooterMessage":
          $("#footermessagecell").html(message);
          break;
          default:
        }
      });
    });
  });
</script>

在IE中,我需要做些什么?基于带有[object Object]的消息框,我假设.find正在IE中工作但由于我无法使用[0]索引到数组或检查它的长度我猜这意味着.find不是返回任何结果。有什么理由可以在Firefox和Chrome中完美运行但在IE中失败?

Is there something I need to do differently in IE? Based on the message box with [object Object] I'm assumed that .find was working in IE but since I can't index into the array with [0] or check it's Length I'm guessing that means .find isn't returning any results. Any reason why that would work perfectly in Firefox and Chrome but fail in IE?

我是一个jQuery的新手,所以我希望我没有做过愚蠢的事情。上面的代码被从论坛中删除,并根据我的需要进行了修改。由于jQuery是跨平台的,我认为我不必处理这个混乱。

I'm a total newbie with jQuery so I hope I haven't just done something stupid. That code above was scraped out of a forum and modified to suit my needs. Since jQuery is cross-platform I figured I wouldn't have to deal with this mess.

编辑:我发现如果我在Visual Studio 2008中加载页面并运行它然后它将在IE中工作。事实证明,它在运行开发Web服务器时始终有效。现在我想IE只是不喜欢在我的本地驱动器上加载的XML中执行.find所以当它在实际的Web服务器上时它可以正常工作。

I've found that if I load the page in Visual Studio 2008 and run it then it will work in IE. So it turns out it always works when run through the development web server. Now I'm thinking IE just doesn't like doing .find in XML loaded off of my local drive so maybe when this is on an actual web server it will work OK.

我已经确认从Web服务器浏览时它工作正常。必须是IE的特色。我猜这是因为Web服务器为xml数据文件传输设置了mime类型,而没有IE没有正确解析xml。

I have confirmed that it works fine when browsed from a web server. Must be a peculiarity with IE. I'm guessing it's because the web server sets the mime type for the xml data file transfer and without that IE doesn't parse the xml correctly.

推荐答案

检查响应的内容类型。如果将messages.xml作为错误的mime类型,Internet Explorer将不会将其解析为XML。

Check the content type of the response. If you get messages.xml as the wrong mime type, Internet Explorer won't parse it as XML.

要检查内容类型,您需要访问XMLHttpRequest对象。正常成功回调不会将其作为参数传递,因此您需要添加通用ajaxComplete或ajaxSuccess事件处理程序。这些事件的第二个参数是XMLHttpRequest对象。您可以在其上调用getResponseHeader方法来获取内容类型。

To check the content type, you need access to the XMLHttpRequest object. The normal success callback doesn't pass it as a parameter, so you need to add a generic ajaxComplete or ajaxSuccess event handler. The second parameter for those events is the XMLHttpRequest object. You can call the getResponseHeader method on it to get the content type.

$(document).ajaxComplete(function(e, x) {
    alert(x.getResponseHeader("Content-Type"));
});

不幸的是,我在Internet Explorer中无法覆盖服务器发送的内容,所以如果它是错误的是你需要更改服务器以发送内容类型的text / xml。

Unfortunately there's no way that I know of in Internet Explorer to override what the server sends, so if it's wrong you need to change the server to send "text/xml" for the content type.

某些浏览器有一个 overrideMimeType 您可以在发送之前调用以强制它使用text / xml的方法,但据我所知,Internet Explorer不支持这种方法。

Some browsers have a overrideMimeType method that you can call before send to force it to use "text/xml", but Internet Explorer doesn't support that as far as I know.

这篇关于jQuery .find()不会在IE中返回数据,但会在Firefox和Chrome中返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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