2011-04-07 23 views
1

有人可以幫我理解我會怎樣寫這個。從xml解析返回的數據中寫入條件

如果我像從一個XML文件解析數據:

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

我怎麼會寫這樣檢查foo =招呼,然後返回再見,有產量,但另有剛剛返回富的聲明?

回答

0

OK..try this。

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

我可能誤解了,但它聽起來像你想爲每個()的回調裏面的條件。你可以這樣做:

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>'); 

}); }