Master AJAX: Part 5: Manipulating DOM Using JavaScript Instant Update Web Page

xiaoxiao2021-04-08  331

April 27, 2006

The previous Brett introduces the document object model (DOM), and its elements define the web page behind the scenes. He will further explore the DOM in this article. Learn how to create, delete and modify the various parts of the DOM tree, learn how to implement web page instant updates!

If you read the previous article, you are very clear about everything happening behind the web page when the web browser shows the web. As mentioned earlier, the web page is converted into an object model from the text when HTML or CSS defined for the page is sent to a web browser. This is the case no matter the simple or complex code, it is the case. Then the browser directly uses the object model instead of the text file you provide. The model used by the browser is called the document object model (Document Object Model, DOM). It connects to the objects of elements, properties, and text in document. All styles, values, and even most spaces in HTML and CSS are merged into the object model. The specific model of a given web page is called the DOM tree of the page.

Understand what is the DOM tree, and know how it means that HTML and CSS are just the first step in controlling the web page. Next, you also need to know how to handle the DOM tree of the web page. For example, if an element is added to the DOM tree, this element will immediately appear in the user's web browser - do not need to reload the page. Remove some text from the DOM tree, those text will disappear from the user screen. You can modify the user interface via the DOM or interact with the user interface, which provides strong programming capabilities and flexibility. Once you have learned how to deal with DOM trees, you have taken a big step towards achieving rich, interactive dynamic sites.

Note, the following discussion is based on the article "Web response using DOM". If you have not read it, please read the previous article before you continue to read.

Crossiler, cross-language

The document object model is a W3C standard (see Referring to the link). Therefore, all modern web browsers support DOM - at least to some extent. Although different browsers have some differences, if Dom core features are used and pay attention to a few special circumstances and exceptions, the DOM code can be used in any browser in the same way. Modifying the code of the Opera webpage can also be used for Apple's Safari®, Firefox®, Microsoft® Internet Explorer® and Mozilla®.

DOM is also a specification across language, in other words, most mainstream programming languages ​​can use it. W3C defines several language bindings for DOM. A language bind is to let you use the DOM API for a particular language. For example, you can use the DOM language binding defined by C, Java, and JavaScript. Therefore, DOM can be used from these languages. There are several language bindings for other languages, although many are defined by third parties other than W3C.

This series of articles mainly discuss the DOM binding of JavaScript. This is because most asynchronous application development needs to write JavaScript code running in a web browser. Using JavaScript and DOM can instantly modify user interfaces, respond to user events and inputs, etc. - is completely standard JavaScript.

In short, it is recommended that you also try the DOM bindings in other languages. For example, using Java language binding not only handles HTML, but also can process XML, which will be discussed in later articles. Therefore, the techniques described herein can also be used in other languages ​​other than HTML, other environments other than client JavaScript.

Node concept

The node is the most basic object type in the DOM. In fact, you will see this article, all other objects defined by the DOM are extension of node objects. However, before analyzing semantics, you must understand the concepts represented by nodes, and then the specific properties and methods of the node are very simple. In the DOM tree, basically everything is a node. Each element is a node in the DOM tree on the bottom. Each attribute is a node. Each text is a node. Even annotations, special characters (such as copyright symbol ©), DOCTYPE declaration (if you have any words in HTML or XHTML) all nodes. Therefore, you must clearly grasp what is a node before discussing these specific types.

The node is ...

In the simplest, anything in the DOM tree is a node. The reason why the word "things" is blurred because it can only be clear to this extent. For example, elements in HTML (such as IMG) and text fragments in HTML (such as "scroll down for more details) do not have much significant similarities. But this is because you may consider the function of each type, focusing on their differences.

But if you observe from another perspective, each element in the DOM tree has a father, this parent node may be another element (such as the child of IMG nested in P element), or DOM tree The top layer element (this is a special case in each document, even if the HTML element is used). In addition, elements and text have a type. Obviously, the type of element is element, the type of text is text. Each node has some definition structure: Is there any nodes (such as child elements) below? Is there a brother node (adjacent to elements or text "" node)? Which document belongs to each node?

Obviously, most of the content sounds abstract. In fact, the type of an element is that the element seems to be a bit stupid. However, we must truly recognize the value of the node as a common object type, must be considered abstract.

Universal node type

The most common task in the DOM code is to navigate in the DOM tree in the page. For example, you can locate a Form through its "ID" attribute, then start processing the elements and text in the form in the Form. It may contain text descriptions, labels, real INPUT elements, and other HTML elements (such as IMG) and links (A elements). If the elements and text are completely different, you must write a completely different code for each type.

If you use a generic node type, it is different. At this time, you only need to move from one node to another, and you need to consider the type of node only when you need a particular processing for the element or text. If you move only in the DOM tree, you can move to the element's parent node or child node using the same operation as the other node type. Only when the special nature of a certain node type is needed, such as elements' properties, the node type is required to be dedicated. Watch all objects in the DOM tree as nodes can simplify operations. After remember this, let's take a specific look at what the DOM node structure will provide, first starting with the properties and methods.

Node's properties

Some properties and methods are required when using the DOM node, so we first discuss the properties and methods of nodes. The properties of the DOM node are mainly:

NodeName Report Name of Node (see hereina). NodeValue provides a "value" of nodes (see later described later). ParentNode returns the parent node of the node. Remember, each element, attribute, and text have a parent node. ChildNodes is a list of children's nodes in nodes. For HTML, the list is only meaningful for elements, and there is no child in text nodes and attribute nodes. Firstchild is just a shortcut to the first node in the ChildNodes list. Lastchild is another shortcut that represents the last node in the ChildNodes list. Previoussibling returns the node before the current node. In other words, it returns the node in front of the node in the ChildNodes list of the parent node of the current node (if confusing, rereading the previous sentence). NextSibling is similar to the Previoussibling property, returns the next node in the ChildNodes list of the parent node. Attributes is only used for element nodes, returns a list of properties of elements. Other minority properties are actually only used for more general XML documents, and there is not much place in processing HTML-based webpages.

Uttrand

The significance of most of the above properties is clear, except for NodeName and NodeValue properties. We don't simply explain these two properties, but two strange problems: What should the nodename of the text node? Similarly, what should the element NodeValue?

If these problems are difficult to take over, you have already understood the vague of these attributes. NodeName and NodeValue are actually not applicable to all node types (so that other minority properties of nodes). This illustrates an important concept: any of these properties may return null values ​​(sometimes called "undefined" in JavaScript). For example, the NodeName property of the text node is a null value (or is called "undefined" in some browsers) because there is no name in the text node. If you expect, NodeValue returns the text of the node.

Similarly, the elements have nodename, namely the element name, but the NodeValue attribute value of the element is always empty. Attributes have NodeName and NodeValue. The next section I will also discuss these individual types, but because these attributes are part of each node, it is necessary to mention here.

Take a look at the list 1, it uses some node properties.

Listing 1. Using the node properties in the DOM

// THESE FIRST TWO LINES GET THE DOM TREE for The Current Web Page,

// and daml> Element for That Dom Tree

Var myDocument = document;

Var htmlelement = myDocument.documentelement;

// What's the name of the Element? "Html"

Alert ("The Root Element of the Page IS" HTMLELEMENT.NODENAME);

// Look for the ELEMENT

Var headelement = htmlelement.getlementsBytagname ("Head") [0];

IF (headelement! = null) {

Alert ("We Found The Head Element, Named" HeadeElement.NodeName); // Print Out The title of the page

Var titleElement = headelement.getlementsBytagname ("Title") [0];

IF (titleElement! = null) {

// TEXT WILL BE FIRST CHILD NODE OF TITLE> ELEMENT

VAR Titletext = TitleElement.firstChild;

// we can get the text of the text node with nodevalue

Alert ("The page title is '" titleText.NodeValue "'");

}

// after IS

Var boodlelement = headelement.nextsibling;

While (BodyElement.nodeName.tolowerCase ()! = "body") {

BodyElement = bodyElement.nextsibling;

}

// We found the Element ...

// We'll Do More When We know Some Methods on the nodes.

}

Node method

Next, look at all nodes have a method (like the node properties, I omitted a few ways to actually do not apply to most HTML DOM operations):

INSERTBEFORE (NewChild, ReferenceNode) Put the NewChild node before the ReferenceNode. Remember, this method should be called for NewChild's target parent nodes. Replacechild (Newchild, Oldchild) replaces the OldChild node with the NewChild node. RemoveChild (Oldchild) removes the OldChild node from the node running the method. Appendchild (Newchild) Adds NewChild into the node running the function. NewChild is added to the end of the target node child list. HaschildNodes () returns true when you call the node of the method, otherwise returns false. Hasattributes () Returns true when calling the node of the method, otherwise returning false.

Note that all of these methods are handled in most cases. This is their main use. If you just want to get a text node value or an element name, you don't need to call these methods, you can use the node properties. Listing 2 Adds the method to use on the list 1.

Listing 2. Node method in DOM

// THESE FIRST TWO LINES GET THE DOM TREE for The Current Web Page,

// and daml> Element for That Dom Tree

Var myDocument = document;

Var htmlelement = myDocument.documentelement;

// What's the name of the Element? "Html"

Alert ("The Root Element of The Page IS" HTMLELEMENT.NODENAME); // Look for the Element

Var headelement = htmlelement.getlementsBytagname ("Head") [0];

IF (headelement! = null) {

Alert ("We Found The Head Element, Named" HeadeElement.NodeName);

//Print out the title of the page

Var titleElement = headelement.getlementsBytagname ("Title") [0];

IF (titleElement! = null) {

// TEXT WILL BE FIRST CHILD NODE OF TITLE> ELEMENT

VAR Titletext = TitleElement.firstChild;

// we can get the text of the text node with nodevalue

Alert ("The page title is '" titleText.NodeValue "'");

}

// after IS

Var boodlelement = headelement.nextsibling;

While (BodyElement.nodeName.tolowerCase ()! = "body") {

BodyElement = bodyElement.nextsibling;

}

// We found the Element ...

// Remove All the top-level Elements in The Body

IF (BodyElement.HaschildNodes ()) {

For (i = 0; i

Var currentnode = bodyElement.childNodes [i];

IF (currentnode.nodeename.tolowercase () == "img") {

BodyElement.RemoveChild (currentnode);

}

}

}

}

have a test!

At present, although only two examples, list 1 and 2, you should be able to learn what you can do with the DOM tree through these two examples. If you want to try this code, you only need to drag your list 3 into an HTML file and save it, and open it with a web browser.

Listing 3. HTML files containing JavaScript code using DOM

JavaScript and the dom </ title></p> <p><script language = "javascript"></p> <p>Function test () {</p> <p>// THESE FIRST TWO LINES GET THE DOM TREE for The Current Web Page,</p> <p>// and death <html> element for what Dom Treevar myDocument = document;</p> <p>Var htmlelement = myDocument.documentelement;</p> <p>// What's the name of the <html> Element? "Html"</p> <p>Alert ("The Root Element of the Page IS" HTMLELEMENT.NODENAME);</p> <p>// Look for the <head> ELEMENT</p> <p>Var headelement = htmlelement.getlementsBytagname ("Head") [0];</p> <p>IF (headelement! = null) {</p> <p>Alert ("We Found The Head Element, Named" HeadeElement.NodeName);</p> <p>//Print out the title of the page</p> <p>Var titleElement = headelement.getlementsBytagname ("Title") [0];</p> <p>IF (titleElement! = null) {</p> <p>// TEXT WILL BE FIRST CHILD NODE OF TITLE> ELEMENT</p> <p>VAR Titletext = TitleElement.firstChild;</p> <p>// we can get the text of the text node with nodevalue</p> <p>Alert ("The page title is '" titleText.NodeValue "'");</p> <p>}</p> <p>// after <head> IS <body></p> <p>Var boodlelement = headelement.nextsibling;</p> <p>While (BodyElement.nodeName.tolowerCase ()! = "body") {</p> <p>BodyElement = bodyElement.nextsibling;</p> <p>}</p> <p>// We found the <body> Element ...</p> <p>// Remove All the top-level <img> Elements in The Body</p> <p>IF (BodyElement.HaschildNodes ()) {</p> <p>For (i = 0; i <bodyElement.childnodes.length; i ) {</p> <p>Var currentnode = bodyElement.childNodes [i];</p> <p>IF (currentnode.nodeename.tolowercase () == "img") {</p> <p>BodyElement.RemoveChild (currentnode);</p> <p>}</p> <p>}</p> <p>}</p> <p>}</p> <p>}</p> <p></ script></p> <p></ hEAD></p> <p><body></p> <p><p> JavaScript and Dom Are a Perfect Match.</p> <p>You can read more in <i> Head rush ajax </ i>. </ P></p> <p><img src = "http://www.headfirstlabs.com/images/hraj_cover-150.jpg" /> <input type = "button" value = "TEST ME!" onclick = "test ();" /></p> <p></ body></p> <p></ html></p> <p>After loading the page into the browser, you can see the screen shown in Figure 1.</p> <p>Figure 1. Use the button to run the HTML page of JavaScript</p> <p>Click Test ME! Will see the warning box shown in Figure 2.</p> <p>Figure 2. Warning box using NodeValue displaying element name</p> <p>After the code is running, the image will be deleted from the page in real time, as shown in Figure 3.</p> <p>Figure 3. Remove the image in real time using JavaScript</p> <p>API design problem</p> <p>Take a look at the properties and methods provided by various nodes. For those who are familiar with object-oriented (OO) programming, they explain an important feature of the DOM: DOM is not fully opposite the API of the object. First, in many cases the properties of the object directly instead of calling the node object. For example, there is no getNodeName () method, but directly use the NodeName property. So the node object (and other DOM objects) is used by attribute rather than the function.</p> <p>Second, if you are used to using the overloaded object and object-oriented API, especially Java and C , you will find that the objects and methods in the DOM are a bit strange. The DOM must be available for C, Java, and JavaScript (this is just a few languages), so API design has made some compromise. For example, there are two different forms: NamedNodeMap methods:</p> <p>GetNamedItem (String Name) getnameditemns (node ​​node)</p> <p>It looks very strange for Oo programmers. The two methods are the same, but only one use String parameter and the other uses the Node parameter. Most OO APIs use the same method names for both versions. The virtual machine running code will determine which method runs based on the object type passed to the method.</p> <p>The problem is that JavaScript does not support this technique called method overload. In other words, JavaScript requires each method or function to use a different name. Therefore, if there is a method of accepting a string parameter called getnamedItem (), no further method or function is also named GetNameDItem (), even if the parameter type of this method is different (or a set of parameters ). If this is done, JavaScript will report an error, and the code will not be executed in the expected manner.</p> <p>Fundamentally, DOM consciously avoids method overloading and other OO programming techniques. This is to ensure that the API can be used in a variety of languages, including those that do not support OO programming technology. The consequence is just asking you to remember some method names. Benewhere is to learn DOM in any language, such as Java, and clear the same method name and encoding structure can also be used in other languages ​​with DOM implementation, such as JavaScript.</p> <p>Let programmers carefully</p> <p>If you go deep into the API design or just pay attention to the API design, you may ask: "Why can't the node type attributes do not apply to all nodes?" This is a good problem, the answer is closer, and the relationship between the problem is closer. Non - technical causes. Simply put, the answer is, "Who knows! But a little annoyed, isn't it?"</p> <p>Attribute NodeName means that each type of node is allowed to have a name, but many cases are either unfained, or for the internal name of programmer's meaningless (for example in Java, many cases of NodeName is reported as #Text "). Fundamentally, you must assume yourself to handle errors. Direct access to mynode.nodeename and then use this value is dangerous, in many cases this value is empty. Therefore, like the usual programming, the programmer should be cautious. Universal node type</p> <p>Some features and properties (as well as some strange places) have now been introduced, and some of the special node types you will use will now. Only four node types are used in most web applications:</p> <p>The document node represents the entire HTML document. Element nodes represent HTML elements such as A or IMG. The property node represents the properties of the HTML element, such as HREF (A element) or SRC (IMG element). The text node represents the text in the HTML document, such as "Click On the Link Below for A Complete Set List". This is a text that appears in the elements of P, A or H2.</p> <p>When processed HTML, 95% of the time is to deal with these node types. Therefore, the rest of this article will discuss these nodes in detail. (Some of the other node types will be introduced when XML will be discussed.)</p> <p>Document node</p> <p>The first node type that is basically all DOM-based code is the document node. The document node is actually not an element in the HTML (or XML) page but is the page itself. Therefore, in the HTML web page, the document node is the entire DOM tree. In JavaScript, you can use keyword document to access document nodes:</p> <p>// THESE FIRST TWO LINES GET THE DOM TREE for The Current Web Page,</p> <p>// and daml> Element for That Dom Tree</p> <p>Var myDocument = document;</p> <p>Var htmlelement = myDocument.documentelement;</p> <p>The Document keyword in JavaScript returns the DOM tree of the current web page. From here you can start processing all the nodes in the tree.</p> <p>You can also create a new node using the Document object, as shown below:</p> <p>CreateElement (ElementName) creates an element using a given name. CreateTextNode (Text) creates a new text node using the provided text. CreateAttribute (AttributeName) Create a new attribute with the supplied name.</p> <p>The key here is that these methods create nodes, but they are not attached or inserted into specific documents. Therefore, this step must be done using the methods described earlier such as INSERTBEFORE () or appendchild (). Therefore, you can create new elements using the following code and add it to the document:</p> <p>VAR pelement = myDocument.createElement ("p");</p> <p>Var text = MyDocument.createtextNode ("Here's Some Text In A P element.");</p> <p>Pelement.Appendchild (Text);</p> <p>BodyElement.Appendchild (pelement);</p> <p>Once you use the Document element to get access to the web page DOM tree, you can use the elements, properties, and text.</p> <p>Element node</p> <p>Although the element node will be used, many of the operations that need to be performed on the elements are all methods and properties allocate, not the specific methods and properties of the element. Elements have only two sets of proprietary methods:</p> <p>Methods related to attribute processing:</p> <p>GetaTtribute (name) Returns the attribute value named Name. RemoveAttribute (name) Deletes the properties called Name. SetAttribute (Name, Value) Creates a property called Name and sets its value to Value. GetaTtributeNode (Name) Returns the property node named Name (Introduction to the Properties Node). RemoveAttributeNode (Node) Deletes the property node that matches the specified node. Methods related to finding nested elements:</p> <p>getElementsBytagname (ElementName) Returns a list of elements nodes with the specified name.</p> <p>These methods are clear, but still come to see a few examples.</p> <p>Processing attribute</p> <p>The processing element is simple, such as the use of Document objects and the above method: a new IMG element:</p> <p>Var imgelement = document.createElement ("IMG");</p> <p>Imglement.setttribute ("src", "http://www.headfirstlabs.com/images/hraj_cover-150.jpg");</p> <p>Imgenement.setttribute ("width", "130");</p> <p>Imglement.setttribute ("HEIGHT", "150");</p> <p>BodyElement.Appendchild (Imgelement);</p> <p>It should now look very simple. In fact, as long as it understands the concept of nodes and knows what methods are available, it will be found that DOM in the web page and JavaScript code are very simple. In the above code, JavaScript creates a new IMG element that sets some properties and then adds to the body element of the HTML page.</p> <p>Find nested elements</p> <p>It is easy to find nested elements. For example, the following code is used to discover and delete all IMG elements in the HTML page shown in Listing 3:</p> <p>// Remove All the top-level <img> Elements in The Body</p> <p>IF (BodyElement.HaschildNodes ()) {</p> <p>For (i = 0; i <bodyElement.childnodes.length; i ) {</p> <p>Var currentnode = bodyElement.childNodes [i];</p> <p>IF (currentnode.nodeename.tolowercase () == "img") {</p> <p>BodyElement.RemoveChild (currentnode);</p> <p>}</p> <p>}</p> <p>}</p> <p>You can also use getElementsBytagname () to complete similar features:</p> <p>// Remove All the top-level <img> Elements in The Body</p> <p>Var imgelements = bodyElement.getElementsByTagname ("IMG");</p> <p>For (i = 0; i <imgelements.length; i ) {</p> <p>Var imgelement = imgelements.Item [i];</p> <p>BodyElement.removeChild (Imgelement);</p> <p>}</p> <p>Property node</p> <p>DOM represents the property as a node, you can access the properties of the elements through the elements, as follows: // remove all the top-level <img> Elements in the body <IMG> Elements in the body</p> <p>Var imgelements = bodyElement.getElementsByTagname ("IMG");</p> <p>For (i = 0; i <imgelements.length; i ) {</p> <p>Var imgelement = imgelements.Item [i];</p> <p>// Print Out Some Information About this Element</p> <p>Var msg = "Found an img element!";</p> <p>Var atts = imgelElement.attribute;</p> <p>For (j = 0; j <atts.length; j ) {</p> <p>Var att = atts.Item (j);</p> <p>Msg = msg "/ n" att.nodeename ": '" att.nodevalue "" "</p> <p>}</p> <p>Alert (msg);</p> <p>BodyElement.removeChild (Imgelement);</p> <p>}</p> <p>The attributes of the attribute have some special places for DOM. On the one hand, the attributes don't actually be like other elements or texts. In other words, the attribute does not appear in the element "under". At the same time, attributes are obviously related to the elements, and the elements "have" attributes. The DOM uses nodes to represent attributes and allows the properties to be accessed through the specific list of elements. Therefore, the attribute is part of the DOM tree, but usually does not appear in the tree. There is reason to say that the relationship between the attributes and the other parts of the DOM tree is a bit blurred.</p> <p>It should be pointed out that Attributes properties are actually a node type rather than being limited to element type. It is a bit weird, does not affect you write code, but it still needs to know this.</p> <p>Although attribute nodes can also be used, it is often simple to process properties using an element class. These include:</p> <p>GetaTtribute (name) Returns the attribute value named Name. RemoveAttribute (name) Deletes the properties called Name. SetAttribute (Name, Value) Creates a property called Name and sets its value to Value.</p> <p>These three methods do not need to directly process the properties node. However, it is allowed to use simple string property settings and delete properties and their values.</p> <p>Text node</p> <p>The last node that needs to be considered is the text node (so at least when processing the HTML DOM tree). Basically, all attributes that are usually used to process text nodes are node objects. In fact, usually use the NodeValue property to access text nodes, as shown below:</p> <p>Var pelements = bodyElement.getElementsByTagname ("P");</p> <p>For (i = 0; i <pelements.lendth; i ) {</p> <p>Var pelement = pelements.Item (i);</p> <p>Var text = pelement.firstchild.nodevalue;</p> <p>Alert (Text);</p> <p>}</p> <p>A few other methods are specifically used for text nodes. These methods are used to increase or decompose data in nodes:</p> <p>AppendData (text) will be added to the existing content provided by the text to the text node. INSERTDATA (Position, Text) allows data to be inserted in the middle of the text node. Insert the text provided at the specified location. ReplaceData (Position, Length, Text) Starts the specified length character from the specified location, replacing the deleted text with the supplied text. What node type? Most of the code you see so far assume that what types already knowing is, but the situation is not always the case. For example, if you navigate and process a general node type in the DOM tree, you may not know that you have or text. Perhaps all children of the P element, but cannot be determined whether the text, B element or IMG element. In this case, it is necessary to determine what type of node before further processing. Fortunately, it is easy to do. The DOM node type defines some constants, such as Node.Element_Node is a constant representing the type of element node. Node.attribute_Node is a constant that represents the type of property node. Node.text_node is a constant that represents the type of text node. Node.Document_Node is a constant that represents the type of document node. There are other node types, but it is rarely used in HTML except these four kinds. I have no value for these constants, although these values ​​are defined in the DOM specification, never use those values ​​directly because this is the purpose of constants! The NodeType property can use the NodeType property comparison node and the constant-this property definition, therefore can be used for all nodes on the DOM Node type, as shown below:</p> <p>Var someode = document.documentelement.firstchild;</p> <p>IF (Somenode.NodeType == Node.Element_Node) {</p> <p>Alert ("We've Found An Element Node Named" Somenode.NodeName);</p> <p>} else if (Somenode.NodeType == Node.Text_Node) {</p> <p>Alert ("It's a text node; the text is" Somenode.NodeValue);</p> <p>} else if (Somenode.NodeType == node.attribute_node) {</p> <p>Alert ("it's an attribute name Somenode.NodeName</p> <p> "with a value of '" Somenode.NodeValue "'");</p> <p>}</p> <p>This example is very simple, but illustrates a big problem: getting the type of node is very simple. More challenging is to determine what to do after the type of node, as long as the node, text, attributes, and element types provide what properties and methods, you can program it yourself. Ok, it's over. The setback NodeType attribute in practice seems to be a hossip of the node - allowing to determine the type of node to be processed and then write code processing the node. The problem is that the above Node constant definition cannot be used correctly for Internet Explorer. So if you use node.ext_node, node.text_node or any constant in your code, Internet Explorer will return the error as shown in Figure 4. Figure 4. Internet Explorer Report Error Node constant in JavaScript, Internet Explorer is an error. Because most people are still using Internet Explorer, it should be avoided using node.ext_node or node.text_node in the code. Although it is said that the upcoming new version of Internet Explorer 7.0 will solve this problem, it is still necessary to have many years before Internet Explorer 6.x exits the stage. It should therefore be avoided using Node, and you want your DOM code (and Ajax applications) to all major browsers. This is important. End language is prepared to become a top web designer? If you are ready to understand, you will become the top web programmer. Most web programmers know how to write images with JavaScript scroll or extract values ​​from the form, some can even send requests and reception responses to the server (you can do it after reading the first few articles of this series). But the gallbowells or no experience cannot make instant to modify the web structure. You have already studied a lot in the previous articles in this series. Now, you should not sit again. Next article, I look forward to me for introducing a variety of smart DOM trees. The current homework is to see how to create an imaginative effect or a beautiful interface using DOM. Experimental and practice in the use of knowledge learned in the past few articles. See if you can establish a website that is more close to the desktop application, and the object can respond to the user's movement on the screen. It is best to draw a boundary for each object on the screen so that you can see where the object in the DOM tree is, then move the object. Create a node and add it to an existing child list, delete an empty node without nested nodes, change the CSS style of the node to see if the child's node will inherit these modifications. The possibility is unlimited. Whenever some new things have been tried, some new knowledge is learned. Modify your web page! In the last article of the DOM trilogy, I will introduce how to combine some great interesting DOM applications into programming. I will no longer talk to and explain the API from the concept, but will provide some code. Before you have, let your own intelligence, see what reference information can be done.</p> <p>You can see this article in our website on our world. Read the introduction of the first few periods of this developerWorks series of articles:</p> <p>"AJAX introduction: Understand the principle of AJAX and its working principle," Part 1 demonstrates how various Ajax technology work together, introduces the core concept of Ajax, including XMLHttpRequest objects (December 2005) . "Use JavaScript and AJAX to send asynchronous requests": Part 2 illustrates how to create an XMLHttpRequest instance, construct, and send requests, and respond to servers (January 2006) in a cross-browser. "Advanced Requests and Responds in Ajax": Part 3 describes how standard Web forms interact with Ajax, and how to master HTTP status, ready state, and XMLHttpRequest objects (February 2006). WEB response using DOM: Part 4 introduces DOM, explaining how HTML is converted into an object model to make web pages in response and interactivity (March 2006). Philip McCarthy writes an Ajax for Java developers: Building a dynamic Java application: From Java's angle to the server side in the server side, a creative method of creating a dynamic web application experience (DeveloperWorks, Sep 2005). Philip McCarthy writes a JAVA-oriented Java Object Sequence for Java Developers: Discussed five ways of serialization of Java objects, analyzing how to send objects through the network and interaction with Ajax (DEVELOPERWORKS, Oct 2005). James Snell writes SOAP Web services, Part 1: Build a web service client: This advanced article describes how to combine Ajax with existing SOAP-based Web services, explain how to use Ajax design mode implementation SOAP web service client based on web browsers (DeveloperWorks, October 2005). Ajax: a New Approach to Web Applications: This article invented the name of Ajax, all AJAX developers should read it. DOM homepage of the World Wide Web: This is the origin of everything related to DOM. The Dom Level 3 Core Specification: Defines the core document object model, from the usage of available types and properties to DOM in different languages. ECMAScript language bindings for Dom: If you are a JavaScript programmer, you want to use DOM in your code, you might interested in this appendix for Level 3 Document Object Model Core. DeveloperWorks Web Architecture Zone: Improve your web build skills through these articles, tutorials and forums. DeveloperWorks Technical Events & Network Broadcasting: Follow these software conferences for technology developers. Get products and technology</p> <p>Brett McLaghlin Head Rush Ajax (O'Reilly Media, Inc., March 2006): In-depth, printing the ideas in this article into your mind. Java and XML, SECOND Edition (Brett McLaughlin, August 2001, O'Reilly Media, Inc.): Take a look at the discussion of XHTML and XML conversion. JavaScript: The Definitive Guide (David Flanagan, November 2001, O'Reilly Media, Inc.): In-depth understanding of the various suggestions using JavaScript and dynamic web pages. The next version will increase two chapters about AJAX. Head First HTML with CSS & XHTML (Elizabeth and Eric Freeman, December 2005, O'Reilly Media, Inc.): Learn more about standard HTML and XHTML, and how to apply CSS to HTML. IBM trial software: Develop your next project with these software, you can download it directly from DeveloperWorks. Discuss DeveloperWorks Blogs: Join the developerWorks community.</p> <p>About author</p> <p>Brett McLaghlin began using a computer from the LOG era. (Remember that small triangle?) In recent years, he has become one of the most popular authors and programmers in Java and XML communities. He has implemented complex enterprise systems in Nextel Communications, written in application servers in Lutris Technologies, recently written and edit this book in O'Reilly Media, Inc.. Brett's latest book Head Rush Ajax, awarded award-winning innovative innovative innovative HEAD FIRST method for Ajax. His collections Java 1.5 Tiger: a developer's Notebook is the first monograph on this Java technology. Classic work Java and XML is still one of the authority of XML technology in Java language.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-132917.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="132917" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.037</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'OI1x22jGTwpKPt6d4yea_2FPaYPZL4njcW0vq3IhWwD4JiX45oMD12U8NXmZVHl2tPbbzTgSIPsFm5ErpAWB6qJA_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>