Way One: Element.attribute
Mode two: Element.getattribute ("attribute");
Way three: Element.classname;
A: Preface
Because I have read a Zhang Xin Xu's blog, remember that there is a reference to getattribute compatibility issues, but the specific can not think of it, I test it first, but, the coincidence is so simple, small woman I use the class attribute of the element, and then fall into the pit without consciousness.
The result of the test is that Ele.getattribute ("class") in the IE7 (including) the following version of the popup is null, think it is a compatibility problem, find a day on the Internet solution, and finally in no way, Will Zhang Xin Xu of that article read again, see finally, internal injuries countless Ah, inside mentioned GetAttribute and Dom This is not a matter of, at that time did not understand, think later learned Dom time again see, results of ... Oh, yes. Forget it, don't tell me.
Two: The difference between the above three kinds of ways
All in the code, see for yourself ...
Html:
<!DOCTYPE HTML Public "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/TR/xhtml1/DTD/ Xhtml1-transitional.dtd "><HTMLxmlns= "http://www.w3.org/1999/xhtml"><Head><title>Kingwell</title><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" /><Body><DivID= "Idheader"class= "Class-header"title= "Kingwell"Status= "1"></Div><labelID= "Forusername" for= "UserName"title= "Kingwell"Status= "1"></label></Body></HTML>
Javascript:
varel = document.getElementById ("Idheader"); Alert (El.getattribute ("id")); alert (el.id); //IE Firfox->idheaderalert (El.getattribute ("Class")); //ie6,ie7, null Ie8,ie9,firefox->class-headeralert (El.class); //ie6,ie7,ie8-> Error ie9,firefox->undefinedAlert (El.getattribute ("ClassName")); //Ie6,ie7->class-header; Ie8,ie9,firefox, NULLalert (el.classname); //All- Class-header varElfor = document.getElementById ("Forusername"); Alert (Elfor.getattribute ("For")); //ie6,ie7->undefined Ie8,9,firefox->forusenamealert (elfor. for ) //IE6,IE7 error, other for undefinedalert (elfor.title)//all output Kingwellalert (elfor.status); //ie6-8, 1 ie9,firefox->undefinedalert (Elfor.getattribute ("Status")) //all output 1 /*Summary: 1: General properties suggest using node. Xxxx. 2: Custom properties suggest using Node.getattribute ("XXXX"). 3: It is recommended to use Node.getattribute ("XXX"), such as the for in the label, when getting the target is the keyword in JS. 4: When acquiring the target is reserved word, such as: class, please use ClassName instead. */
This summary is not my results, but there is a small mistake in the middle, has been changed;
To express respect for the original, here is the link:
http://kingwell-leng.iteye.com/blog/1663553
The acquisition of element attributes in JS