Write conditionals from data returned of an xml parse
Can someone please help me understand how 开发者_开发知识库I would write this.
If i am parsing data from an xml file like:
function parseXml(xml) {
$(xml).find("ITEM").each(function()
{
var foo= $("bar", this).text();
$("#container").append('<div>' + (foo) + '</div>');
});
}
How would I write a statement like if foo = hello then return goodbye and have that output but otherwise just return foo?
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>');
});
}
I could have misunderstood, but it sounds like you want a condition inside the callback for each(). Could you do something like:
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>');
}); }
精彩评论