2011-08-12 43 views
0

我想要獲取XML響應中元素的所有屬性的數組。JQuery - 獲取XML元素的所有屬性

$(xData.responseXML).find("[nodeName=z:row]").each(function() { 
    console.info($(this).attr("ows_Title")); 
    ... 

這將返回正確的值爲ows_Title,但我想找出所有的z:行的屬性。我該怎麼做,並讓它在所有瀏覽器中都能正常工作?我有一個FF和Chrome的工作方法,但它不適用於IE。 IE似乎沒有認識到XML元素具有屬性,但是當我專門尋找像「ows_Title」之類的元素時,它會看到它們。

這個什麼:

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

這並不做任何事情,在IE瀏覽器,即使它有一個NamedNodeMap中出現,當我做console.info(this.attributes):

for(var key in this.attributes) { 
    alert("test"); 
    ... 

回答

1

想通了。我結束了迭代。

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; 
    } 
} 
0

嘗試:

$(xData.responseXML).find("[nodeName=z:row]").each(function() { 
    console.info(this.attributes); 
... 
+0

它提出了一個具有正確數量屬性的「NamedNodeMap」 - 我該怎麼做? – Christian

+0

我在原文中增加了另外一件東西 - 請看「這是怎麼回事:」部分。 – Christian

+0

你能用你的完整例子創建一個jsfiddle.net嗎? – ChristopheCVB