我有一个HTML DIV元素:
<div class="obj" height="this is attr 1" rel="this is att2" width="this is att3"></div>
我有一个新变量:
attArray
:var attArray = new Array();
我想一步一步将
div.obj
中的每个att变成attArray
。我该怎么做?attArray[0] = "this is attr1"
attArray[1] = "this is attr2"
attArray[2] = "this is attr3"
最佳答案
简单:
$('.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