Full-stack JavaScript (11) Learning Attr nodes

Source: Internet
Author: User

Element features are represented by Attr nodes in the DOM. You can access the Attr constructor and prototype in all browsers.

Technically, an Attr node is a node in the Attrbutes attribute of an element. The constructor function is; function Attr () {[native code]}

Although it is also called a node, the Attr type node is not part of the document tree!


Features of Attr nodes:

  • NodeType: 2
  • NodeName: name of the feature
  • NodeValue: feature value
  • PrasentNode: null
  • In HTML, subnodes are not supported
  • In XML, nodes of the Text and EntityReference types are supported.



The three most common methods for operating Attr nodes;

  • GetAttribute ()
  • SetAttribute ()
  • RemoveAttribute ()

In addition, getAttributeNode (), setAttributeNode (), removeChild ()


Attr nodes have three attributes: name, value (value equivalent to nodeValue), and specified (specified is a Boolean value used to distinguish special
Is it specified in the Code or the default .)


Create the Attr node document. createAttribute (). After creation, you can only use el. setAttribute () to add the feature to the element.

Var arr = document. createAttribute ('align ';) arr. value = "center"; element. setAttributeNode (arr); // The setAttributeNode () method must be used.

You are not advised to directly access the feature node. It is best to access the attribute node through the method described above.


Note: adding attributes directly to the DOM does not become a feature of html elements.

Div. mycolor = "red"; // it does not become a feature of the div element. alert (div. getAttribute ("mycolor"); // null (except IE)


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.