The acquisition of element attributes in JS

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.