What about the property?
Are you experiencing some problems when you try to figure out how to deal with properties? Before you mention, the property does have your own object type, but the property does not show the child elements of its elements, nested elements and text are not in the same property "level", you will notice that Listing 2 and 3 Answers There is no display attribute.
The attribute is in fact stored in the object model used by the browser, but they have some special situations. Each element has a list of available properties and is separated from the sub-object list. So the DIV element may have a list of "id" and another attribute "class".
Remember, the properties of the elements must have the only name, that is, one element cannot have two "id" or two "class" properties. This makes the list easy to maintain and access. In the next article, you will see that you can simply call methods such as GetAttribute ("ID") to obtain the value of the attribute by name. You can also use a similar method to add a value of an attribute or setting (reset) existing attribute. It is worth noting that the uniqueness of the attribute name makes the list different from the list of child objects. The P element may have multiple EM elements, so the child object list can contain multiple repetitions. Although the operation mode of the sub-list and the attribute list is similar, one can contain duplicate (sub items for objects), and one cannot (attributes of the element object). Finally, only the elements have attributes, so the text object does not have additional lists for storing properties.
Before proceeding, talk about how the browser converts the tag to a tree, there is a topic worth exploring, that is, how to handle the browser is not a formatted tag. The format is a widely used term in XML, there are two basic elections:
Each start tag has a matching end tag. So each
matches p> in the document, each
In-depth study of these two rules. These two rules not only simplify the organization of documents, but also eliminate uncertainty. Is it necessary to apply a bold after bold? Or just the opposite? If this order and umbellar are not a big problem, please remember that the CSS allows the rules to overwrite other rules, so, for example, if the text of the text in the B element is different from the font in the I element, the format application order will change It is very important. Therefore, the format of HTML has a good role.
If the browser receives a document that is not a format, it will only do our best. The tree structure will be approximate to the original page of the author's hopes, worst unrecognizable. If you have filed the page into the browser, you can see how you should have the structure when you see the browser results and continue to work in frustration. Of course, it is quite simple to get this question: Make sure the document is well format! If you don't know how to write standardized HTML, please consult the reference information to get help.
So far, you already know that the browser converts the web page to an object, maybe you will even guess, the object is DOM tree. Dom said Document Object Model is a specification that can be obtained from World Wide Web Consortium (W3C) (you can see some DOM related links in the reference).
Every item is a node
Obviously, the Document object is important, but this is just starting. Before further in-depth, you need to learn another term: node. You already know that each part of the tag is represented by an object, but it is not just an arbitrary object, it is a specific type of object, a DOM node. More specific types, such as text, elements, and attributes inherit from this basic node type. So there are text nodes, element nodes, and attribute nodes.
Var Domtree = Document; var phoneenumbelement = document.getlementByid ("Phone"); var phoneenumber = phonenumbelement.value;
Here, I want to leave you a little suspense. Obviously, this is not a complete detailed description of the DOM; in fact, this article is just the introduction of the DOM. The content of the DOM is far more than what I introduced today!
Before reading the next article, try to think about the tree structure and use some of your own HTML practice to see how the web browser converts HTML to tagged tree views. In addition, think about the organization of the DOM tree, and practice with the special circumstances introduced herein: attribute, there is a text in which the element is mixed, and there is no element of the text (such as an IMG element).
And don't forget, there are answers to lists 2 and 3 here, which also contain sample code!
Figure 2. Answer of Listing 2
Figure 3. Answer of Listing 3
You can see this article in our website on our world. Learn the first few articles on this developerWorks series of Ajax: