JavaScript Dom action method (4)--document node method

Source: Internet
Author: User

(1) Reading and writing methods

Document.open ()   //For new and open a document Document.close ()   //uneasy than the open method of the newly created document document.write ()   // Used to write content to the current document Document.writein ()  //used to write to the current document, trailing add line breaks.

(2) Finding nodes

Document.queryselector (selectors)//accepts a CSS selector as a parameter, returning the first element node that matches the selector. Document.queryselectorall (selectors)//accepts a CSS selector as a parameter, returning all element nodes that match the selector. document.getElementsByTagName (tagName)//Returns all elements of the specified HTML tag document.getelementsbyclassname (className)// Returns an element that includes all class names that match the specified criteria Document.getelementsbyname (name)//To select an HTML element that has the name attribute (for example<form>、<Radio>、<img>、<Frame>、<Embed>And<Object>document.getElementById (ID)//Returns the element node that matches the specified ID attribute. Document.elementfrompoint (x, y)//Returns the element child node at the top of the page's specified position. 

1.document.queryselector (selectors)//accepts a CSS selector as a parameter, returning the first element node that matches the selector.

Document.queryselectorall (selectors)//accepts a CSS selector as a parameter, returning all element nodes that match the selector.

<DivID= "text">    <DivID= "Test1">Test 1</Div>    <DivID= "All"class= "All">          <I>Dream Dragon 1</I>          <Pclass= "box">              <emclass= "span">Dream Dragon 2</em>          </P>          <Iclass= "span">Dream Dragon 3</I>          <Pclass= "box">              <em>Dream Dragon 4</em>          </P>      </Div>  </Div><Buttononclick= "myFunction ()">Point Me</Button><Scripttype= "Text/javascript">    functionmyFunction () {//gets the text content of the element with the class name Test1        vartext=document.getElementById ("text"). Queryselector ("#test1");        alert (text.textcontent); //gets all <i> elements of <div> in the class named All, similar to getElementsByTagName ("I")        varI=document.getElementById (" All"). Queryselectorall ("I"); //alert (i[1].textcontent);        //get all elements with a class name of span        varspan=Document.queryselectorall (". Span"); //get all <em> elements in all <p> tags        varem=Document.queryselectorall ("p em"); }</Script>

2.document.elementfrompoint (x, y)//Returns the element child node at the top of the page's specified position.

<p> User name View code file (online)</p>
 <button  id= "btn"> Coordinates (100,100) will change color.</Button><Script>document.getElementById ("btn"). onclick= function() {Document.elementfrompoint ( -,  -). Style.color= "#cd0000"; };</Script>

The effect is as follows:

JavaScript Dom action method (4)--document node method

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.