Home  >  Article  >  Web Front-end  >  JavaScript implementation method of simply traversing all attributes of DOM objects_javascript skills

JavaScript implementation method of simply traversing all attributes of DOM objects_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:35:561279browse

The example in this article describes the implementation method of traversing all attributes of DOM objects in JavaScript. Share it with everyone for your reference, the details are as follows:

HTML of DOM object:

Copy code The code is as follows:
ad37381c2519e89362baf630524eba50Disable State Cookie65281c5ac262bf6d81768915a4a77ac0

1. Traverse all attributes of the DOM object (all attributes will be traversed regardless of whether they are set in the HTML tag)

var obj=document.getElementById("btnToggleState") 
var s="";
for(var elem in obj)
   s+=elem+"\n";
alert(s);

2. Traverse the attributes set in the HTML tag of the DOM object

$.each($('#btnToggleState')[0].attributes,function(i,attrib){
  alert(i+":"+attrib.name+":"+attrib.value);
});

I hope this article will be helpful to everyone in JavaScript programming.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn