<Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" /><title>Untitled Document</title></Head><Body><inputtype= "text"ID= "UserName"/><imgsrc= "User.png" /> </Body><Scripttype= "Text/javascript">/*Find tags by relationship (parent-child relationship, sibling relationship). ParentNode Gets the parent node of the current element. ChildNodes gets all the next-level child elements of the current element. FirstChild Gets the first child node of the current node. LastChild gets the last child node of the current node. ------------------------------------------------------------nextSibling Gets the next node of the current node. (Brother node) previoussibling gets the previous node of the current node. (Brother node) we can filter by the type of the tag: type of text node: 3 node type of note: 8 Type of label node: 1*/ varBodynode=document.getElementsByTagName ("Body")[0]; /*View the parent node. var parentnode = Bodynode.parentnode; Alert ("Parent node Name:" +parentnode.nodename); Find child nodes: ChildNodes gets all the child nodes and returns an array. Note: When you get a child node, you include empty text or a comment. var children = Bodynode.childnodes; for (var i = 0; i<children.length; i++) {//if (children[i].nodetype==1) {alert ("Node name:" +CHILDREN[I].N Odename+ "Type of object:" +children[i].nodetype); }} alert ("First child node:" +bodynode.firstchild.nodename); Alert ("Last child node:" +bodynode.lastchild.nodename); Find a Brother node*/ varInputnode=document.getElementById ("UserName"); Alert ("Next sibling node:"+inputNode.nextSibling.nodeName); //Next sibling node. Alert ("Previous sibling node:"+inputNode.previousSibling.nodeName); </Script></HTML>
JavaScript uses relationships to find tags