Full-stack JavaScript (24) DOM2 and DOM3, which do not involve XML namespace extension, dom2dom3

Source: Internet
Author: User

Full-stack JavaScript (24) DOM2 and DOM3, which do not involve XML namespace extension, dom2dom3

(1) three attributes are added to the DocumentType change: publicId, systemId, and internalSubset (internal subset)


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"[<!ELEMENT name (#PCDATA)>] >

You can access the file by using document.doctype.publicId,document.doctype.systemId,document.doc type. internalSubset.


Export document.doc type. internalSubset will get "<! ELEMENT name (# PCDATA)> ". This internal subset (internal subset) is rarely used in HTML and may be more common in XML.


(2) importNode () method, which is unrelated to xml naming, is used to retrieve a node from a Document and put it in another Document to become a part of the Document.

Note that each node has an ownerDocument. If the appendChild (node) method is used, an error is returned if the input node is not in the same document.


The importNode () method is similar to the cloneNode () method. It accepts two parameters: one is the time to import, and the other is a Boolean value, indicating whether to import subnodes.


Var newNode = document. importNode (oldNode, true) // import all child nodes document. body. appendChild (newnode)


In DOM2, The View module adds a unique attribute, defaultView, pointing to the document window (or framework ). There is a Level Attribute parentWindow in IE. If you want to confirm the window of the document:

var parentWindow = document.defaultView || document.parentWindow



In addition to the importNode () method and the defaultView attribute, two methods are added:

Document. implementation. createDocumentType (document type name, publicId, systemId). Because the existing document type cannot be modified, you can only call this method when creating a new document.

Document. implementation. createDocument (namaspaceURL, root, doctype );


Example:

var doctype = document.implementation.createDocumentType("html","-//W3C//DTD XHTML 1.0 Strict//EN","http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd")

var doc = document.implementation.createDocument("http://www.w3.org/1999/xhtml","html", doctype);



(3) a namespace-independent method is added for Node type changes. The isSupported () method is similar to the document. hasFeature () method in dom1 to determine the capabilities of the current Node.

However, we recommend that you use capability testing.

Example:

if(document.body.isSupported('html','2.0')) {  //...}

DOM3 also references isSameNode (), isyunnode (),

Both methods accept a node parameter and return true if the input node is the same or equal to the referenced node.

The so-called "same" means that two nodes reference the same object.

Equality means that two nodes are of the same type and have the same attributes (nodeName, nodeValue, and so on ), and their attributes and childNodes attributes are also equal (the same position contains the same value ).

DOM3 also introduces a new method setUserData () for adding additional data to the Node. This method will specify the data to the Node and accept three parameters: Key, value, and processing function.

document.body.setUserData("name","100",function(opration,key,value,srcNode,destNode){})

Then use var v alue = document. body. getUserData ("name") to obtain the value.


SetUserData (), callback function, with 5 parameters,

  • Operation Type: 1 copy, 2 Import, 3 Delete, 4 rename
  • Data key
  • Data Value
  • Source node
  • Target Node

The processing function (callback function) in setUserData () is called when a node with data is copied, deleted, renamed, or introduced into a document, therefore, you can determine in advance how to process user data when the preceding operations occur.


(4) framework changes


Although the framework and the internal framework are represented by HTMLFrameElement and HTMLIframeElement, they both have a new attribute in dom2: contentDocument

Var iframe = document. getElementById ("myIframe"); var iframeDoc = iframe. contentDocument; // IE8 is invalid

A conentDocument object is an instance of the Document type, so you can use it like a document Object, including all attributes and methods.

IE8 does not support the contentDocment attribute of the framework, but supports the contentWindow attribute.

Therefore:

var iframe = document.getElementById("myIframe");var iframeDoc = iframe.contentDocument || iframe.contentWindow.document;


 




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.