jQuery XML解析-检查元素是否具有属性

jQuery XML解析-检查元素是否具有属性,jquery,xml,Jquery,Xml,你能帮我解决一些XML文档的问题吗 当解析文档时,我只需要获取相关数据 下面是我正在循环的一组子元素的示例: <element type="forecast_icon_code">3</element> <text type="precis">Partly cloudy.</text> <text type="probability_of_precipitation">10%</text> $(locationInfo).

你能帮我解决一些XML文档的问题吗

当解析文档时,我只需要获取相关数据

下面是我正在循环的一组子元素的示例:

<element type="forecast_icon_code">3</element>
<text type="precis">Partly cloudy.</text>
<text type="probability_of_precipitation">10%</text>

$(locationInfo).children().each(function(){
alert($(this).attr("forecast_icon_code"));
});
3
部分多云。
10%
$(locationInfo).children().each(函数()){
警报($(this).attr(“预测图标代码”);
});
返回值为“未定义”

请执行以下操作:

var attr = $(this).attr("forecast_icon_code");
if (typeof attr !== 'undefined') {
    ..do something with it
}


漂亮的简单和金钱。我使用$(this).text()来获取基于attr的元素值,niceone!
if ($(this).is('[forecast_icon_code]')) {
    ..have the attribute
}