JS Common method gets element, node parent-child relationship Lookup

Source: Internet
Author: User
Tags tag name

/*

* Common way to get elements

* 1. Get element document.getElementById (' attribute name ') by ID

* 2. Get the element document.getelementsbytagname (' attribute name ') by signing it;

* 3. Use the Class property to get the element with compatibility problem Document.getelementsbyclassname (' class attribute name '); get an array

* 4. There is a compatibility problem with the Document.getelementsbyname (' name Attribute name ') by using the Name property to get the element; Returns an array

/*

* Node Parent-child relationship Lookup

* 1. Get all the child nodes, including spaces and tabs, etc.

* A. Get elements first

* B. element name. childNodes;

* 2. Get all the label nodes

* element name. Child;

* 3. Get the first child node

* element name. firstchild;

* 4. Get the first tag element

* element name. firstelementchild;

* 5. Get the last child node

* element name. lastchild;

* 6. Get the last tag element

* element name. Laseelementchild

* 7. Gets the previous element of the current element

* element name. previoussibling

* 8. Get the previous LABEL element

* element name. previouselementsibling

* 9. Get the next node

* element name. nextSibling

* 10. Get the next tag element

* element name. nextelementsibling

* 11. Get the parent element from the element

* element name. parentnode;

* 12. Parent element Node

* element name. parentelement

*/

/*

* Quick way to get element nodes

* 1. Get the node document.forms for all forms;

* 2. Get all the A-tag node document.links;

* 3. Quick access to IMG tag nodes

* a.document.images;

* b.document.getelementsbytagname (' tag name ');

*/

/* Actions for tag elements

* 1. Add the attribute of the element, add the class attribute

* a.setattribute (' attribute name ', ' attribute value ');

Property change CSS Style

* B.setattribute (' class ', ' attribute name ');

* 2. Remove removeattribute (' attribute name to remove ');

* 3. Get the attribute getattribute (' attribute name ');

* Operation of label text

* 1. Acquisition of text

* a.innerhtml, you can get the tag attributes and values of the element

* b.textcontent can only get plain text, cannot get the middle label

* 2. Set the value of the text InnerHTML = "value to be assigned"

* Style of operation

* 1. Modification of the style

* A. Gets the properties of the class document. (images,links,forms)

* B. Assigning a value using a For loop

* 2. Get the Style: property name. style. Styles

*/

JS Common method gets element, node parent-child relationship Lookup

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.