开发者

Get each Attribute of element in loop function

开发者 https://www.devze.com 2023-01-21 00:43 出处:网络
I have a HTML DIV element: <div class=\"obj\" height=\"this is attr 1\" rel=\"this is att2\" width=\"this is att3\"></div>

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

0

精彩评论

暂无评论...
验证码 换一张
取 消