In the development of Web applications, especially Web2.0 programs. You often want to get an element in the page, and then update the style, content, and so on for that element. How to get the element to be updated is the first problem to be solved. Thankfully, there are a number of ways to get nodes using JavaScript, and here's a quick summary (the following methods are tested in IE7 and Firefox2.0.0.11):
1. Get through the top-level document node:
(1)document.getElementById (ElementID): The method passes the ID of the node. Can accurately obtain the required elements. is a simpler and quicker method.
Assume that the page contains multiple nodes with the same ID. Then only the first node is returned.
Right now. A number of JavaScript libraries, such as prototype, MooTools, have been shown, and they provide a simpler way to do this: $ (ID), which is still the ID of the node.
This approach can be seen as
The second way to document.getElementById (). Just $ () is more powerful, and the detailed usage method is able to refer to their respective API documentation.
(2)document.getelementsbyname (elementname): This method is obtained by node name, which can be seen from the name. Instead of a node element, this method returns an array of nodes with the same name.
We can then loop through a property of the node to get the required node.
For example, in HTML, the checkbox and radio all use the same name attribute value to identify the elements within a group. Suppose we want to get the selected element now. First get the reorganization element. Then the loop infers whether the checked property value of the node is true.
(3)document.getElementsByTagName (tagName): The method is to get the node through the node's tag, the same method also returns an array, such as:
document.getElementsByTagName (' A ') will return all hyperlink nodes on the page. Before acquiring a node, it is common to know the type of the node, so it is easier 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, does this approach have no practical place? Of course not, this method is different from the above two, it is not a proprietary method of the document node, but also can apply other nodes, the following will be mentioned.
2. Get through parent node:
(1)Parentobj.firstchild: This method can be used if the node is the first child node of a known node (parentobj).
This property can be used recursively, that is, support
ParentObj.firstChild.firstChild.firstChild ... The form. In this way, a deeper node can be obtained. &NBSP
(2) parentobj.lastchild : Very Clearly, This property is the last child node that gets the known node (parentobj). Like FirstChild, it can also be used recursively. &NBSP
in use. Let's say we combine the two together. Then the more exciting effect will be achieved, namely: parentobj.firstchild.lastchild.lastchild...
(3) parentobj.childnodes : Gets an array of child nodes for a known node, and can then find the required node through a loop or index. &NBSP
note: It was found that the array of direct child nodes was obtained on IE7. On Firefox2.0.0.11, you get all child nodes, which are child nodes that contain child nodes. &NBSP
(4) parentobj.children : Gets an array of direct child nodes for a known node. &NBSP
note : tested, on IE7, the same as the childnodes effect. And Firefox2.0.0.11 does not support it.
This is why I have to use different ways than other methods. Therefore, it is not recommended.
(5)parentobj.getelementsbytagname (tagName): Usage does not repeat, it returns an array of child nodes of the specified value in all child nodes of the known node.
Like what:
Parentobj.getelementsbytagname (' A ') returns all hyperlinks in the known child nodes.
3. Get through neighboring nodes:
(1)neighbournode.previoussibling: Gets the previous node of a known node (Neighbournode). This property, like the previous firstchild and LastChild, seems to be able to be used recursively.
(2)neighbournode.nextsibling: Gets the next node of the known node (Neighbournode), with the same support for recursion.
4. Get through child nodes:
(1)Childnode.parentnode: Gets the parent node of the known node.
The method mentioned above. Just a few of the main methods, assuming that JavaScript libraries such as prototype are used, there may be other different approaches. For example, through the node class acquisition and so on. Just assume that you can flexibly apply the various methods above. I believe we should be able to handle most of the procedures.
References: http://blog.csdn.net/bradwoo8621/article/details/1747502
JavaScript is taken? H? T? M? L?? D? O? M-Father?,? son?,? pro-near?