(5) Get and set node attributes

Source: Internet
Author: User

It is easy to get the attributes of a node. ELEM [attrname] seems to be good,

ELEM. getattribute (attrname );

Since it is so simple, it seems unnecessary to introduce it.

But all evil browsers make such a simple thing very complicated.

The attributes described in this article include the following functions:

Setattribute

Getattribute

Hasattribute

Setstyle

Getstyle

1. getattribute

Getattr is mainly compatible with special attributes, such as href SRC. in IE, you must use getattrbute ('src', 2) to obtain the actual content.

2. setattribute

Setattr is used to set element attributes. Generally, you can directly use the system's setattribute.

3. getstyle

Getstyle is not just a simple way to get the value of the node's style attribute, because some CSS attributes are defined in CSS and these attributes are not obtained in the style attribute. For IE, use

Node. currentstyle gets the current property value. For other browsers, use window. computestyle (node, null); get (2nd parameters are pseudo classes). Almost all frameworks are getstyle at the same time, and the attribute names are overwritten by rules, such as font-size-> fontsize, you can easily obtain CSS with the same name.

Some special attributes cannot be obtained normally (for example, auto). In ie, the unit cannot end with PX. Therefore, jquery deals with this situation. The method is to use runtimestyle. pixelleft and runtimestyle. Left to coordinate the calculation. (See jquery source code)

4. setstyle

Setstyle is the value defined in the style. For numbers, you must manually add 'pxy '.

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.