Get each Attribute of element in loop function
I have a HTML DIV element:
<div class="obj" height="this is attr 1" rel="this is att2" width="this is att3"></div>
I've a new Variable: attArray
:
var attArray = new Array();
I want to get step by step each开发者_如何学编程 att in div.obj
into attArray
. How do I do it?
attArray[0] = "this is attr1"
attArray[1] = "this is attr2"
attArray[2] = "this is attr3"
Each element already has an attributes-collection, you can access it like an array.
Simple:
$('.obj').each(function() {
var attArray = [];
for(var k = 0; k < this.attributes.length; k++) {
var attr = this.attributes[k];
if(attr.name != 'class')
attArray.push(attr.value);
}
//do something with attArray here...
});
Working example
精彩评论