In the development of web applications, especially Web2.0 programs, it is often necessary to obtain an element in the page and then update the style, content, etc. of the element. How to get the elements to be updated is the first problem to be solved. It is gratifying that there are many ways to obtain nodes using JavaScript. Here is a brief summary (the following methods were tested in IE7 and Firefox 2.0.0.11):
1. Get through the top-level document node:
(1) (elementId): This method can accurately obtain the required elements through the node ID, which is a relatively simple and fast method. If the page contains multiple nodes with the same id, then only the first node is returned.
Nowadays, multiple JavaScript libraries such as prototype and Mootools have appeared, which provide a simpler method: $(id), and the parameter is still the id of the node. This method can be regarded as
Another way to write (), but the function of $() is more powerful. For specific usage, please refer to their respective API documents.
(2) (elementName): This method obtains the node through the name of the node. From the name, it can be seen that this method returns not a node element, but an array of nodes with the same name. Then, we can loop through the node's attribute to obtain a node to determine whether it is the required node.
For example: in HTML, checkbox and radio both use the same name attribute value to identify elements in a group. If we want to get the selected element now, we first get the shuffled element, and then loop to determine whether the checked attribute value of the node is true.
(3) (tagName): This method obtains the node through the tag of the node. The method also returns an array, for example:
('A') will return all hyperlink nodes on the page. Before acquiring a node, you usually know the type of node, so using this method is relatively simple. But the disadvantage is also obvious, that is, the returned array may be very large, which will waste a lot of time. So, is this method useless? Of course not. This method is different from the above two. It is not a proprietary method of document nodes. Other nodes can also be applied, as mentioned below.
2. Get through the parent node:
(1): This method can be used if the node is the first child node of a known node (parentObj). This property can be used recursively, which means it is supported
In this way, deeper nodes can be obtained.
(2): Obviously, this property is the last child node of the known node (parentObj). Like firstChild, it can be used recursively.
In use, if we combine the two, we will achieve a more exciting effect, namely:...
(3): Get the array of child nodes of known nodes, and then you can find the required nodes through loops or indexes.
Note: After testing, it was found that the array of direct child nodes is obtained on IE7, while the array of child nodes is obtained on Firefox2.0.0.11 is all child nodes, that is, child nodes including child nodes.
(4): Get the direct child node array of known nodes.
Note: After testing, on IE7, the effect is the same as childNodes, but Firefox 2.0.0.11 does not support it. This is also why I want to use different styles from other methods. Therefore, it is not recommended to use.
(5) (tagName): The usage method will not be repeated, it returns an array of child nodes of a known node with a specified value among all child nodes. For example:
('A') Returns all hyperlinks in known child nodes.
3. Obtain through adjacent nodes:
(1): Get the previous node of a known node (neighbourNode). This property seems to be recursively used, just like the firstChild and lastChild in the previous ones.
(2): Get the next node of a known node (neighbourNode), and also supports recursion.
4. Obtain through child nodes:
(1): Get the parent node of a known node.
The methods mentioned above are just some basic methods. If you use JavaScript libraries such as Prototype, you may also get other different methods, such as class acquisition through nodes, etc. However, if the above methods can be flexibly used, I believe that most procedures should be able to cope with.
1. Get through the top-level document node:
(1) (elementId): This method can accurately obtain the required elements through the node ID, which is a relatively simple and fast method. If the page contains multiple nodes with the same id, then only the first node is returned.
Nowadays, multiple JavaScript libraries such as prototype and Mootools have appeared, which provide a simpler method: $(id), and the parameter is still the id of the node. This method can be regarded as
Another way to write (), but the function of $() is more powerful. For specific usage, please refer to their respective API documents.
(2) (elementName): This method obtains the node through the name of the node. From the name, it can be seen that this method returns not a node element, but an array of nodes with the same name. Then, we can loop through the node's attribute to obtain a node to determine whether it is the required node.
For example: in HTML, checkbox and radio both use the same name attribute value to identify elements in a group. If we want to get the selected element now, we first get the shuffled element, and then loop to determine whether the checked attribute value of the node is true.
(3) (tagName): This method obtains the node through the tag of the node. The method also returns an array, for example:
('A') will return all hyperlink nodes on the page. Before acquiring a node, you usually know the type of node, so using this method is relatively simple. But the disadvantage is also obvious, that is, the returned array may be very large, which will waste a lot of time. So, is this method useless? Of course not. This method is different from the above two. It is not a proprietary method of document nodes. Other nodes can also be applied, as mentioned below.
2. Get through the parent node:
(1): This method can be used if the node is the first child node of a known node (parentObj). This property can be used recursively, which means it is supported
In this way, deeper nodes can be obtained.
(2): Obviously, this property is the last child node of the known node (parentObj). Like firstChild, it can be used recursively.
In use, if we combine the two, we will achieve a more exciting effect, namely:...
(3): Get the array of child nodes of known nodes, and then you can find the required nodes through loops or indexes.
Note: After testing, it was found that the array of direct child nodes is obtained on IE7, while the array of child nodes is obtained on Firefox2.0.0.11 is all child nodes, that is, child nodes including child nodes.
(4): Get the direct child node array of known nodes.
Note: After testing, on IE7, the effect is the same as childNodes, but Firefox 2.0.0.11 does not support it. This is also why I want to use different styles from other methods. Therefore, it is not recommended to use.
(5) (tagName): The usage method will not be repeated, it returns an array of child nodes of a known node with a specified value among all child nodes. For example:
('A') Returns all hyperlinks in known child nodes.
3. Obtain through adjacent nodes:
(1): Get the previous node of a known node (neighbourNode). This property seems to be recursively used, just like the firstChild and lastChild in the previous ones.
(2): Get the next node of a known node (neighbourNode), and also supports recursion.
4. Obtain through child nodes:
(1): Get the parent node of a known node.
The methods mentioned above are just some basic methods. If you use JavaScript libraries such as Prototype, you may also get other different methods, such as class acquisition through nodes, etc. However, if the above methods can be flexibly used, I believe that most procedures should be able to cope with.