How to generate dynamic content with DOM-2 1

xiaoxiao2021-03-06  60

Recently, peer-to-peer network distribution calculations, and people who have re-ignited real-time data, have made this hot topic to show dynamic content in the Web. Unfortunately, HTTP's nomadical and limitations of rendering components in different browsers, bringing new information to clients and not wanting to send additional requests to the server to bring significant challenges. .

In order to address this challenge, the developer returns to JavaScript skills, such as smartly using the document.write () statement, frameset, and iframe, or using Internet Explorer's InnerHTML () and INSERTADJACENTHTML () functions into new HTML into new HTML. Content, this state continues until recently. Although these methods are successful in some extent, they are usually coupled to each other with a particular browser. A specific drawback of these technologies is to rarely consider the structure of the document as a problem. In most cases, just simply inserting another HTML string into the page, rarely considering what is actually rendering.

Fortunately, with the emergence of the W3 browser, we can now have a better way. The secondary document object model (DOM-2) supported by the IE5 and NS6 browsers (DOM-2) provides an interface that developers can instantly generate new HTMLs after the page load is complete. By calling the DOM-2 method, you can create an HTML element, define the properties of the element, and attach the element to the document or existing elements, thereby implementing an instant generation of HTML. This article will explore some of the basic functions of DOM-2 in the browser to generate dynamic content.

This article assumes that you have a general understanding of the correct document structure and understand the concept of the document as a series of HTML objects with parent-child relationships.

Node and elements

All items that make up a document are called nodes in the DOM-2 specification. The node interface provides a range of common methods and properties that enable developers to access and operate all items in the document.

Like NS6 (Mozilla), IE5 puts all items or identifies all items in the HTML document, or identifies the text node. It is very important to understand the basic differences of elements and text nodes. The elements are generally associated with the identity surrounded by sharp brackets. At HTML, all identities are elements, such as

, , and

. Elements may also have attributes, or contain sub-nodes.

On the other hand, the text node represents a piece of text. It is different from the elements, no attributes or sub-nodes (although it inherits the ChildNodes and attribute sets from the node interface).

Consider the following example code:

Partly Cloudy, Scattered Showers, High 67

The above code consists of two independent nodes. The identifier is an element node with an id attribute. The text in the span is actually an independent text node. This node only has text, no attribute or sub-node.

Element nodes and text nodes have some common attributes:

NodeType: This property saves a numeric value, corresponding to the type of node. The nodetype attribute value of the element is 1, and the NodeType property value of the text node is 3. In operation, if several unknown type nodes are needed, you can use this property to distinguish a specific node type. NodeName: This property saves a string. Like NodeType, this string and node type correspondence. All text nodes of the NodeName property value is "#text". For the element, its nodeName property value contains the name of the element identifier. Therefore, an nodeEname property value of an HTML image identifier is "IMG". NodeValue: This property saves the value of the node, if any. The nodevalue attribute value of the element is NULL. The NodeValue value of the text node is the actual text string containing the node. If you need a complete list of node properties and methods, you can refer to the DOM-2 specification for W3C.

Create elements and text

A series of methods included in the document object make the creation of the new node possible. These methods are:

CreateElement: Create new elements for the specified type and return a reference to this element. CreateTextNode (String text): Create a new text node with the content specified by the Text parameter and returns a reference to the node.

CreateElement

Create a new element node unexpectedly simple. As long as the CreateElement method is called, it is possible to pass the element type you want. If you wish to perform further operations on this new element, the return value of the method - the reference to this new element will be a good idea.

The following code created a new paragraph element with no content:

Var newpara = document.createElement ("p");

Now the newpara variable is a reference to the new paragraph element.

CreateTextNode

You can create a text node in a similar manner, call the CreateTextNode method, and get the text string you want to put into this new node. As in creating elements, you can save the return value into a variable to reference in the subsequent code.

VAR newText = Document.createTextNode ("this is a new callense.");

The previous code creates two new nodes, but these two nodes cannot be seen in the browser because they have not been inserted into the document. The new node will be freely floated in JavaScript until you assign them to a father object, or the document body (body) object itself.

Every object from the node interface has a method called appendchild, which allows other nodes to be inserted into this node. For example, by using an AppendChild method, you can insert NewText's new text node into paragraph elements named Newpara:

Var newpara = document.createElement ("p");

VAR newText = Document.createTextNode ("this is a new callense.");

Newpara.Appendchild (NewText);

Now, what you need to do is to insert paragraph elements into the document object. To achieve this, you need a reference to the body of the Body element of the document. There are several ways to get this reference. Most comparison new browser versions (IE5 and NS6) allow you to use document.body statements, such as Document.Body.Appendchild (NewPara); however, this is a non-standard convenience property that may not work in future versions. A more compliant method is to get a reference to the first Body element you can find in the document through the getElementSbyTagName method of the document object.

Var bodyref = document.getElementsBytagname ("body"). Item (0);

Bodyref.Appendchild (NewPara);

Method ITEM (0) is necessary because the getElementSbyTagName function returns its collection of elements found according to the specified identification name. Since there is usually only one body ID in an HTML page, you can use the ITEM method to get the first item from the collection.

There is also a method that follows the standard method to get a reference to the document, which is to use the getElementByid method. However, this method assumes that you are not afraid of trouble, assign an ID attribute for your HTML page's ID.

Var bodyref = document.getlementByid ("DocBody");

Bodyref.Appendchild (NewPara);

You can also use the getElementsByTagName and getElementByid to get references to other page objects other than document objects. For example, clicking this connection will activate a script that uses the getElementByid method to get a reference to this paragraph element in this article (this paragraph element ID attribute is specified as "eXample1"), then inserted into this paragraph element through the appendchild method. A new text node.

The code to complete the above tasks is as follows: