簡體   English   中英

在IE6中使用jQuery解析XML的問題

[英]Issues parsing XML with jQuery in IE6

我試圖以跨瀏覽器兼容的方式使用jQuery從xml中提取值。 我在firefox中沒有任何問題,但是不幸的是,這也必須與IE兼容。

我的jQuery代碼如下所示:

$(document).ready(function()) {
  $.get("file.xml", {}, function(parseRefreshTime){
    alert('This line is executed in IE.');  
    $("created", parseRefreshTime).each(function() {
      alert('This line is *not* executed in IE.');  
      refreshTime = $(this).text();
      //do stuff with refreshtime
    });
  });
});

這將為我的xml文件中的<created>節點提取節點值。

我在頁面中引用了jQuery庫,並且在Firefox中可以正確解析,因此我假設解析代碼是適當的。 我在Firefox中收到兩個警報,但在IE中只有第一個。

我可以發誓昨天我有非常相似的代碼可以工作,但是我必須進行一些調整並以某種方式破壞了它。 與它戰斗了近一個小時之后,我正在尋找另一組眼睛。

有人可以在這里發現我在做什么錯嗎?

一些東西:

  • 為您的AJAX請求指定響應類型為xml
  • 將返回的XML對象包裝在$(doc)並使用find查詢XML
  • 我認為您在第一行中有一些錯別字: reader應該已經ready並且您在括號中有一個額外的括號

這在IE6上對我有用。 如果這對您不起作用,則可能要調查您是否正確提供了xml。

index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Test</title>

  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> 
  <script type="text/javascript" charset="utf-8">
    $(document).ready(function() {
      $.get("test.xml", null, function(doc) {
        $(doc).find('created').each(function() {
          alert($(this).text());
        })
      }, 'xml');
    });
  </script>
</head>
<body>

</body>
</html>

test.xml

<?xml version="1.0" encoding="UTF-8"?>
<created>2010-01-07 00:00:00</created>

嘗試用$()包裝parseRefreshTime

    $("created", $(parseRefreshTime)).each(function() {
      alert('This line is *not* executed in IE.');  
      refreshTime = $(this).text();
      //do stuff with refreshtime
    });

或嘗試使用$(parseRefreshTime).find('created')

    $(parseRefreshTime).find("created").each(function() {
      alert('This line is *not* executed in IE.');  
      refreshTime = $(this).text();
      //do stuff with refreshtime
    });

更新:另外,嘗試將type指定為xml

$.get("file.xml", {}, <callback>, "xml")

確保將“ text / xml”用作xml文件的內容類型。

我正在使用這樣的東西:

    if ($.browser.msie){
        var tempXML = new ActiveXObject("Microsoft.XMLDOM");
        tempXML.async = false;
        tempXML.loadXML(data);
        xmlc = tempXML;
        items = $($(xmlc)[0]);
    } else if(window.DOMParser){
        items = $(new DOMParser().parseFromString(data, "text/xml").childNodes[0]);
    } else {
        xmlc = data;
        items = $($(xmlc)[1]);
    }

基本上,嘗試使用IE的Microsoft.XMLDOM方法。 您可以提供示例xml嗎?

XML和IE6的最大難題之一就是字符編碼。 確保您的瀏覽器可以正確解釋文件。 與文檔本身相比,您的網絡服務器很可能為頁面提供了不同的編碼標頭。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM