JavaScript DOM Basic operations

Source: Internet
Author: User

JavaScript DOM Basic operations

1.DOM (Document Object model)

2. Node:

    • Documentation node: Document
    • ELEMENT nodes: HTML, head, body, title, meta, H1, p, etc.
    • Attribute node: attr
    • Text-node: text

2.1 Node Acquisition

    • Documen.getelementbyid ("Element ID")//Returns the element containing the ID
    • Document.getelementsbyname ("element name attribute")//Returns the collection with the specified name element
    • document.getElementsByTagName ("element tag")//Returns a collection of specified elements

2.2 Attribute node processing

    • Elementnode. Hasattribute ("Attrname")//detects if the selected element has a Attrname attribute eg:documen.getElementById ("Nav"). Hasattribute ("class")
    • Elementnode. getattribute ("Attrname")//Gets the value of the element Attrname property
    • Elementnode. SetAttribute ("Attrname", "attrValue")//Set properties, create or change element property values
    • Elementnode. RemoveAttribute ("Attrname")//Remove attributes
    • Note: When getting properties and removing attributes, use the Hasattribute () method to determine whether this property exists

2.3 Text Node processing

    • Get and set text node properties through the innerHTML property
    • Documen.getelementbyid ("Content"). Innerhtml= "<p>hello!</p>"

3 traversing the DOM

    • Parent node. FirstChild//Gets the first child node of the element
    • Parent node. LastChild//Gets the last child node of the element
    • Parent node. Childnode//Get element child node list
    • Child nodes. parentnode//Get a known node parent node
    • Sibling node. previoussibling//Get the previous node of a known node
    • Brother node. nextSibling//Get a node after a known node

4. Node operation

JavaScript DOM Basic operations

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.