Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the types of JavaScript attribute nodes

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "what are the types of JavaScript attribute nodes". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

In JavaScript, the attribute node refers to the attribute object, which represents an attribute of the HTML element; the constant of the attribute node is expressed as "ATTRIBUTE_NODE" and represents a constant value of "2", which can be used to determine the node type.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

DOM (Document Object Model) is the document object model. DOM technology can be used to realize the dynamic changes of web pages, such as dynamically showing or hiding an element, changing their attributes, adding an element, and so on. DOM technology greatly enhances the interaction between users and web pages.

DOM node type

Nodes in the DOM tree can be classified in different ways. According to the hierarchy of nodes, it can be divided into: ancestor node (the general name of all nodes above the current node), parent and child node (representing the relationship between the upper and lower nodes), descendant node (the general name of all nodes under the current node) and sibling node (all nodes with the same parent node) and so on.

According to the node type, it can be divided into: document node, element node, attribute node, text node, comment node. Different types of nodes have a corresponding constant, which represents a specific value. You can use this constant to determine the node type. The constant representation and values of commonly used HTML DOM nodes are shown in the following table.

Table: HTML DOM node type and its constant node type node type constant value document node DOCUMENT_NODE9 element node ELEMENT_NODE1 attribute node ATTRIBUTE_NODE2 text node TEXT_NODE3 comment node COMMENT_NODE8

JS attribute node

In HTML DOM, an attribute node is an attribute object that represents an attribute of a HTML element. An element can have multiple attributes. All attributes of the element are stored in the collection NamedNodeMap that represents the disorder. Nodes in NamedNodeMap can be accessed by name or index.

When you use DOM to work with HTML document elements, sometimes you need to deal with the attributes of the elements, and you need to use the attributes and related methods to the attribute nodes. The common attributes and related methods of attribute nodes are shown in the following table.

Table: common attributes of attribute nodes and related methods attribute / method description nodeName | name is referenced by attribute object and returns the name of element attribute nodeValue | value is referenced by attribute object to set or return the value of element attribute Item (node subscript) returns the number of nodes in the attribute node set specified subscript lengh returns the number of nodes in the attribute node set nodeType returns the type value of the attribute node

Note: the effects of attributes name and nodeName are equivalent, and those of value and nodeValue are equivalent.

Example: manipulate an attribute node.

Operate attribute node document node var oA = document.getElementById ('a1'); var aAttr = oA.attributes;// get all attribute nodes of an element console.log (' an element has the following attribute nodes:'); for (var I = 0; I < aAttr.length; inodes +) {/ / traverse all attribute nodes of an element console.log (aAttre [I]) } console.log ('aAttr [0] node type is:' + aAttr [0] .nodeType); / / get the type value console.log of the first attribute node ('aAttr [0] node name is:' + aAttr [0] .nodeName); / / get the node name console.log of the first attribute node ('aAttr [0] node value is:' + aAttr [0] .nodeValue) / / get the node value of the first attribute node

Elements in the access attribute node list can also use item (), aAttr [0] is equivalent to aAttr.item (0), and aAttr [0] .nodeName is equivalent to aAttr [0] .name, and aAttr [0] .nodeValue is equivalent to aAttr [0] .value. The result of the above code running in the Chrome browser is shown in figure 1.

This is the end of the content of "what are the types of JavaScript attribute nodes". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report