JavaScript dom (two-Document object)

Source: Internet
Author: User

JavaScript represents documents by document type. In the browser, document is an instance of the HTMLDocument object (inherited from document) that represents the entire HTML page. and the document object in the browser is also a property of the Window object, so it can be used as a global property

The document node has the following characteristics:

Nodetype=9

Nodename= "#document"

Nodevalue=null

Parentnode=null

Ownerdocument=null

Its child nodes can make the DocumentType type (only 1 allowed), Element (up to 1), processinginstruction,comment.

Document represents HTML or other XML documents. The most common is the Document object as a HTMLDocument instance, through which not only the page information can be manipulated, but also the appearance and background structure of the page can be manipulated.

1, the Text subdirectory node: DocumentType type (only 1 allowed), Element (up to 1), processinginstruction,comment. There are two properties for the built-in Access child nodes

The DocumentElement property, which always points to the

The ChildNodes property, which accesses the text subdirectory node. Obviously there is only one HTML element under the ChildNodes list in the document


As an example of HTMLDocument, document also has a property that always points to <body>: document.body;

alert (document.body.nodeName); // BODY

Output body, pointing to body through document object.


Document another possible child node is DocumentType. Usually the <! The doctype> tag is considered to be a different entity from the rest of the document, and its information is accessed through the DOCTYPE property of documents. Different browsers support this property differently

2. Document Information

Document as the object of HTMLDocument, there are some unique properties.

Title Property: Contains the text in title. You can use Document.title to modify the title content so that it is only modified in the browser display, and the contents of the actual document are unchanged.

URL properties: The full URL, the URL displayed in the Address bar.

Domain property: Contains only the name of the page

Referrer property: Contains the URL of the source page linked to the current page, or an empty string if the current page is the start page

<HTML>  <Head>    <title>Test</title>  </Head>  <Body>    <PID= "PD"name= "Test"><!--Notes -</P>    <Scripttype= "Text/javascript">alert (document.title);//Test      varURL=document.      URL; varDomain=Document.domain; varreferrer=Document.referrer; alert (URL);//where the current page is storedalert (domain);//Emptyalert (referrer);//Empty    </Script>  </Body></HTML>

Because it is a static page, it is not published to the server, so the URL of the domain and source page is empty.


3. Find elements

The most common DOM application is to get a reference to an element, which provides two methods for referencing the element domcument type: getElementById () and Getelementbytagname ()

The getElementById () method receives a parameter ID and returns an element if the corresponding ID is found, otherwise null is returned. The ID is the id attribute of the page element.

The getElementsByTagName () method receives a parameter label name and returns a nodelist that contains 0 or more elements. In an HTML document, the method returns a Htmlcollection object that is similar to the nodelist.

<PID= "PD"name= "Test">The</P>    <PID= "PT">No.2</P>    <Scripttype= "Text/javascript">alert (document.getElementById ("PD"). Firstchild.nodevalue);// thealert (document.getElementById ("PT"). Firstchild.nodevalue);//No.2      varpList=document.getElementsByTagName ("P");  for(varI=0; I<Plist.length;i++) {alert (Plist.item (i). Firstchild.nodevalue); }// the      //No.2    </Script>

4. Special set

Document also has some special collections, all of which are htmlcollection types:

document.anchors//returns all <a> elements with the name attribute in the document

document.forms//return all <form> elements in a document

document.images//return all <image> elements in a document

document.links//returns all <a> elements with the href attribute

These elements can always be accessed through the HTMLDocument object.


5. Consistency detection

The detection browser implements the parts of the DOM, which can be passed the Document.implementation property, DOM1 defines a method for the property hasfeature (), receives two parameters, the name of the DOM feature to be detected, and its version number, if support returns True.

Alert (Document.implementation.hasFeature ("XML", "1.0"));//true

6. Document Writing

Document has the output stream written to the document. The way to implement this feature is write (), Writeln (), open (), close ().

The Write () and Writeln () methods receive a string argument that represents a string that is written to the document, Writeln more than one carriage return. Cannot write directly to "</script>", will directly lead to the end.

Open () and close () are used to turn the document output stream on and off, respectively.

JavaScript dom (two-Document object)

Related Article

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.