JavaScript uses relationships to find tags

Source: Internet
Author: User

<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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.