jquery XML,我需要.html()而不是.text()但不工作?

时间:2010-06-11 20:00:47

标签: jquery xml

我需要var long导出为html而不是文本。我知道我有.text()但是当我使用.html()时它不起作用。另外如果我在声明变量时取出.text()并且它在IE中不起作用?

原因是,在XML中,某些单词会包含 等html标签,我需要识别这些标签。当我拿出.text()时,我以为我解决了它,但后来我看了IE,我一无所获。

$(document).ready(function(){
$.ajax({
type: "GET",
url: "xml/sites.xml",
dataType: "xml",
success: function(xml) {
 $(xml).find('site').each(function(){
  var id = $(this).attr('id');
  var title = $(this).find('title').text();
  var class =$(this).find('class').text();
  $('<div class="'+class+'" id="link_'+id+'"></div>').html('<h2>'+title+'</h2>').appendTo('#page-wrap');
   $(this).find('desc').each(function(){
    var long = $(this).find('long');
    var url = $(this).find('url').text();
    $('<div class="long"></div>').html(long).appendTo('#link_'+id);
    $('<a href="http://'+url+'"</a>').html(url).appendTo('#link_'+id);
   });
 });
}
});
});// JavaScript Document

1 个答案:

答案 0 :(得分:0)

我有类似的问题,我找到了解决方案...在调用ajax函数之前,检查哪个浏览器然后根据那个定义内容类型var ....有prase函数,你根据Microsoft执行其他代码标准和ajax为浏览器的其余部分... IE10现在与xml内容类型兼容...这里是完整的代码...我已经测试了IE 7,它完美的工作...希望这回答你的问题。 ..

  (function ($) {

  $.fn.myplugin = function (CP_ID) {

  var selected_dataType = "";

    if (currentBrowser().browser == "IE" && currentBrowser().version < 10) {

       selected_dataType = "text";
     }
   else {
       selected_dataType = "xml";
   }

  $.ajax({
    type: "GET",
    url: "XML_file.xml",
    dataType: selected_dataType,
    success: function (xml) {

        var processedXML_01 = parseXml(xml);

        $(processedXML_01).find('myRecord').each(function () {
              //process you code//
        });
       },
    error: function () {
        alert("An error occurred while processing XML file.");
    }
   });

   } //end plugin function 

   })(jQuery);


    function parseXml(xml) {

    if (currentBrowser().browser == "IE") {

    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.open("GET", "XML_file.xml", false);
    xmlhttp.send();
    xmlDoc = xmlhttp.responseXML;

    xml = xmlDoc;
   }
        return xml;
   }


function currentBrowser() {

$.returnVal = "";

var browserUserAgent = navigator.userAgent;

if (browserUserAgent.indexOf("Firefox") > -1) {

    $.returnVal = { browser: "Firefox" };
}

else if (browserUserAgent.indexOf("Chrome") > -1) {

    $.returnVal = { browser: "Chrome" };
}

else if (browserUserAgent.indexOf("Safari") > -1) {

    $.returnVal = { browser: "Safari" };
}

else if (browserUserAgent.indexOf("MSIE") > -1) {

    var splitUserAgent = browserUserAgent.split(";");

    for (var val in splitUserAgent) {

        if (splitUserAgent[val].match("MSIE")) {

            var IEVersion = parseInt(splitUserAgent[val].substr(5, splitUserAgent[val].length));
        }
    }

    $.returnVal = { browser: "IE", version: IEVersion };
}

else if (browserUserAgent.indexOf("Opera") > -1) {

    $.returnVal = { browser: "Opera" };
}

else {
    $.returnVal =
     { browser: "other" };
}

return $.returnVal;
}