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.
1. Obtain through the top-level document node:
(1) document.getElementById(elementId): This method can accurately obtain the required information through the ID of the node. element is a relatively simple and quick method. If the page contains multiple nodes with the same ID, only the first node will be returned.
Today, multiple JavaScript libraries such as Prototype and Mootools have appeared. They provide easier methods: $ (ID), and the parameters are still the ID of the node. This method can be regarded as another way of writing document.getElementById(), but the function of $() is more powerful. For specific usage, please refer to their respective API documents.
(2) document.getElementsByName(elementName): This method obtains the node through the name of the node. As can be seen from the name, this method returns not a node element, but an array of nodes with the same name. Then, we can loop through a certain attribute of the node to determine whether it is the required node.
For example: Checkbox and Radio in HTML are identified the elements in a group through the same name attribute value. 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) document.getElementsByTagName(tagName): This method obtains the node through its Tag. This method also returns an array. For example: document.getElementsByTagName('A') will return all hyperlink nodes on the page. . Before obtaining the node, the type of the node is generally known, so it is relatively simple to use this method. 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 two above. It is not a proprietary method of the document node and can also be applied to other nodes, which will be mentioned below.
2. Get through the parent node:
(1) parentObj.firstChild: This method can be used if the node is the first child node of a known node (parentObj). This attribute can be used recursively, that is, it supports the form of parentObj.firstChild.firstChild.firstChild..., so that deeper nodes can be obtained.
(2) parentObj.lastChild: Obviously, this attribute is to obtain the last child node of the known node (parentObj). Like firstChild, it can also be used recursively.
In use, if we combine the two, we will achieve a more exciting effect, namely: parentObj.firstChild.lastChild.lastChild...
(3) parentObj.childNodes: Obtained You know the array of child nodes of a node, and then you can find the required node through looping or indexing.
Note: After testing, it was found that on IE7, what is obtained is the array of direct child nodes, while on Firefox2.0.0.11, what is obtained is all child nodes, including the child nodes of the child node.
(4) parentObj.children: Get the direct child node array of the known node.
Note: After testing, on IE7, the effect is the same as childNodes, but Firefox2.0.0.11 does not support it. This is why I use a different style than other methods. Therefore its use is not recommended.
(5) parentObj.getElementsByTagName(tagName): The usage method will not be described in detail. It returns an array of child nodes of the specified value among all child nodes of the known node. For example: parentObj.getElementsByTagName('A') returns all hyperlinks in known child nodes.
3. Obtain through adjacent nodes:
(1) neighborNode.previousSibling: Get the previous node of the known node (neighbourNode). This attribute seems to be the same as the previous firstChild and lastChild. Used recursively.
(2) neighborNode.nextSibling: Get the next node of the known node (neighbourNode), also supports recursion.
4. Obtain through child nodes:
(1) childNode.parentNode: 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 obtaining through the class of the node, etc. However, if you can flexibly use the various methods above, I believe you should be able to handle most programs
Operation code:
<table> <tr> <td id="TEST"> <input type="submit" value="确定"> <input type="button" value="取消"> </td> </tr> </table>
Tested:
var td= document.getElementById("TEST"); alert(td.childNodes.length);结果为4
JS中,空格也是作为一个文本节点,而两个input元素后面都有空格
所以都作为一个文本节点,所以结果为4
删除空格后结果为2
为了处理里面的空格节点,用以下函数来处理
function cleanWhitespace(element) { for(var i=0; i<element.childNodes.length; i++) { var node = element.childNodes[i]; if(node.nodeType == 3 && !/\S/.test(node.nodeValue)) { node.parentNode.removeChild(node); } } }
The above is the detailed content of Introduction to how to obtain various dom nodes in javascript. For more information, please follow other related articles on the PHP Chinese website!