根据xml解析返回的数据编写条件

Write conditionals from data returned of an xml parse

本文关键字:数据 条件 返回 xml 根据      更新时间:2023-09-26

谁能帮我理解我该怎么写这个?

如果我正在解析来自xml文件的数据,如:

function  parseXml(xml) {
    $(xml).find("ITEM").each(function()
  {
    var foo= $("bar", this).text();
    $("#container").append('<div>' + (foo) + '</div>');
  });
}

我该如何写一个语句,如如果foo = hello,然后返回再见,并有输出,否则只是返回foo?

OK..试试这个。

function  parseXml(xml) {
    $(xml).find("ITEM").each(function()
  {
    var foo = $("bar", this).text();
if(foo == "hello"){ foo = "goodbye" }
 $("#container").append('<div>' + foo + '</div>');
  });
}

我可能误解了,但听起来您希望在each()的回调中有一个条件。你能不能这样做:

function  parseXml(xml) {
$(xml).find("ITEM").each(function()
var foo= $("bar", this).text(), 
    output; 
if (foo === /[Gg]ood(-)*bye/) {
    output = 'Goodbye';
}
else { output = foo; }
$("#container").append('<div>' + (Output) + '</div>');  

});}