Get all attributes of an object - not working in IE
In the following code, I included an attribute for debugging. In FF and Chrome, I get a ton of alerts saying "attribute found", but in IE, I get nothing. The function returns an empty array.
I have also tried removing the console.info(this)
line.
BTW, I'm using SPServices to access lists from SharePoint 2010 -- I'm trying to get all the columns of a list.
/*!
* listAttributes jQuery Plugin v1.1.0
*
* Copyright 2010, Michael Riddle
* Licensed under the MIT
* http://jquery.org/license
*
* Date: Sun Mar 28 05:49:39 2010 -0900
*/
//THIS ISN'T WORKING IN IE
if(jQuery) {
jQuery(document).ready(function() {
开发者_运维知识库 jQuery.fn.listAttributes = function(prefix) {
var list = [];
var attributes = [];
$(this).each(function() {
console.info(this);
for(var key in this.attributes) {
alert("attribute found");
if(!isNaN(key)) {
if(!prefix || this.attributes[key].name.substr(0,prefix.length) == prefix) {
attributes.push(this.attributes[key].name);
}
}
}
list.push(attributes);
});
return attributes;
}
});
}
//end listAttributes Plugin - use this to see what attributes
function ImportSPListColumnsToArray(ListName)
{
var ArrayForStorage = new Array();
$(document).ready(function() {
$().SPServices({
operation: "GetListItems",
async: false,
listName: ListName,
CAMLViewFields: "",
CAMLQueryOptions: "<QueryOptions><ViewAttributes Scope='RecursiveAll'/></QueryOptions>",
**completefunc: function (xData, Status) {
$(xData.responseXML).find("[nodeName='z:row']").each(function() {
//find all fields used by each row and aggregate them without duplicating
var row_attr = $(this).listAttributes();**
for (var i=0; i<row_attr.length; i++)
{
if ($.inArray(ArrayForStorage, row_attr[i]) == -1)
{
ArrayForStorage.push(row_attr[i]);
}
}
row_attr.clear();
});
}
});
});
return ArrayForStorage;
}
I could be wrong, but I believe your problem is the console.info(this);
line. IE does not have this available out of the box (you need to open Developer Tools first), so you're script is failing at that point.
Figured it out.
IE apparently doesn't recognize this.attributes
as having a length in the for each loop.
So I just iterated through them, and it worked.
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;
}
}
精彩评论