开发者

jquery XML, i need .html() not .text() but not working?

I need var long to be exported as html and not text. I know I have .text() but when I use .html() it will not work. Also if I take the .text() out when declaring the variable and it will not work in IE?

The reason for this is, in the XML certain words will have html tags like or and I need those to be r开发者_如何学JAVAecognized. I thought I solved it when I took out .text() but then i looked at IE and I got nothing.

$(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


i have similar problem and i found the solution... before call ajax function, check which browser is and then define content-type var according to that.... the have prase function where you do additional code according to Microsoft standard and ajax for rest of browser ...well IE10 is compatible with xml content-type now ... here is complete code... i have tested up to IE 7 and it works perfectly... hope this answer your question...

  (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;
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜