JQuery - Get all attributes of an XML element
I'm trying to get an array of all the attributes of an element in an XML response.
$(xData.responseXML).find("[nodeName=z:row]").each(function() {
console.info($(this).attr("ows_Title"));
...
This returns the correct value for ows_Title, but I want to find out all the attributes that the z:row has. How can I do that and have it work in ALL browsers? I have a method working for FF and Chrome, but it doesn't work in IE. IE doesn't seem to recognize that an XML element has attributes, but it sees them when I look specifically for one like "ows_Title".
What about this:
for(var key in this.attributes) {
if(!isNaN(key)) {
if(!prefix || this.attributes[key].name.substr(0,prefix.length) == prefix) {
attributes.push(this.attributes[key].name);
}
}
}
THIS DOESN'T DO ANYTHING IN IE, even though it comes up with a NamedNodeMap when I do console.info(this.attributes):
for(var ke开发者_高级运维y in this.attributes) {
alert("test");
...
Figured it out. I ended up just iterating.
if(jQuery) {
jQuery.fn.listAttributes = function() {
var attributes = new Array();
$(this).each(function() {
for (var i=0; i<this.attributes.length; i++)
{
attributes.push(this.attributes.item(i).nodeName);
}
});
return attributes;
}
}
Try :
$(xData.responseXML).find("[nodeName=z:row]").each(function() {
console.info(this.attributes);
...
精彩评论