Dom of AJAX

Source: Internet
Author: User

Dom

In Dom, if you want to access a node with a deep position in the document, or a group of nodes. We can use the following methods for obtaining.
• 1.doc ument. getelementbyid ()
• 2.doc ument. getelementsbyname ()
• 3.doc ument. getelementsbytagname ()
• In the previous PPT, we learned the methods 1 and 3. The 2nd methods are used to obtain all elements whose features are equal to the specified value. Note that an array is returned.
• Example:
• <Input type = "radio" name = "radcolor" value = "red"/> Red
• <Input type = "radio" name = "radcolor" value = "green"/> green
• <Input type = "radio" name = "radcolor" value = "blue"/> blue
• Consider the above sectionCodeThe page will ask the user which color they like. All single-choice buttons use the same name, because only one value is returned for this field.

Use the following code to obtain references to all radio button elements:
• Varoradios = Document. getelementsbyname ("radcolor ");
• Loop processing can be performed.
• For (vari = 0; I <oradio. length; I ++ ){....}
• Note that IE6 has some errors in using this method. First, they will return the element whose ID is equal to the given name. Second, they only check the <input/> and elements.

In addition to directly specifying the features of a node to reference the node, you can also reference the relative node through the relationship between nodes. The following describes each node.

The childnodes attribute allows us to retrieve all the child elements of any element from the node tree of the given document.
• The childnodes attribute returns an array containing all elements of a given element node.
• Usage: element. childnodes
• If You Want To reference all the elements of the document's body element, you can use the getelementsbytagname () method to obtain the body element. Because each document has only one body element. So we use:
• Varbody_element = Document. getelementbytagname ("body") [0];
• Now body_element has pointed to the body element of the document. Next, you can use the syntax tag shown below to retrieve all the elements of the Body element:
• Body_element.childnodes
• PS: The Body element can also be referenced using document. Body.

If you want to reference the HTML node-root node of the document, you can use:
• Document. childnodes [0];
• The • Document. childnodes [0]. childnodes [0]; // reference the • Document. childnodes [1]. childnodes [1]; // reference the <body> node
• In addition to childnodes, Dom also provides attributes for directly referencing subnodes:
• Element. firstchild;
• Element. lastchild;
• They represent the first subnode and the last subnode respectively.

The HTML node hierarchy is a tree structure. Except for the root node, each node has only one parent node and can be referenced by the parentnode attribute. The syntax is element. parentnode.
• For example:
• <Tr>
• <TD id = "TD1">... </TD>
• </Tr>
• We can use:
• Vartdnode = Document. getelementbyid ("TD1 ");
• Get the TD node and then use
• Vartrnode = tdnode. parentnode;
• The TR node can be obtained.

Similar to referencing a parent node, Dom provides attributes for referencing a sibling node. In an HTML document, nodes at the same document level are called sibling nodes.
• <Trid = "tr1">
• <TD id = "TD1">... </TD>
• <TD id = "td2">... </TD>
• </Tr>
• <Trid = "tr2">
• <TD id = "td3">... </TD>
• <TD id = "td4">... </TD>
• </Tr>
• We can use element. nextsibling; to reference the previous sibling node.
• Use element. previussibling; to reference the next sibling node.
• Tr1 and tr2 are brother nodes TD1 and td2 are also brother nodes. Td2 and td3 are not sibling nodes
• If the node does not have a sibling node, the attribute returns NULL.

1. Get the node name from the nodename attribute
• Syntax:
• Node. nodename
• The return value of nodename varies with node types.
• If it is an element node, the flag name is returned. If it is an attribute node, the attribute name is returned. If it is a text node, the text content is returned.

Syntax: node. nodetype;
• The nodetype attribute has 12 optional values, of which only three have practical values: Element nodes, attribute nodes, and text nodes.
• The value of nodetype of an element node is 1.
• The attribute value of nodetype of the attribute node is 2.
• The value of nodetype of a text node is 3.

Syntax: node. nodevalue;
• Different Types of nodes have different return values;
• If the Element Node returns NULL, the attribute node returns undefined, and the text node returns text.
• See example 04.

Use haschildnodes () to determine whether a given node has subnodes.
• Node. haschildnodes ();
• If a subnode returns true, false is not returned.

Use tagname to obtain the tag name of an element node
• This element is a unique attribute of an element node and has the same return value as nodename, that is, the name of the returned tag. I will not introduce it here.

Usage of several common attributes in the drop-down list
The drop-down list is a commonly used object corresponding to <SELECT> </SELECT>. Each drop-down list contains multiple <option> components. The following examples describe the usage of several common attributes.
• 1) Value Attribute
• 2) number of options for obtaining the Length attribute
• 3) use the option attribute to obtain the option set
• Varoptionitem = Document. getelementbyid ("proanalyse ");
• For (vari = 0; I <optionitem. length; I ++ ){
• If (optionitem. Options [I]. value = proanalyseid)
• Optionitem. Options [I]. Selected = true;
•}
• 4) use the value and text attributes of Option
• <Option value = "apple"> Apple </option>
• The value here is "apple", and the text value is "apple"
5) use the selected Attribute
• Example: 3) demo
• Each option object has two States selected and unselected. The values of selected are true and false.
• 6) use the onchange event of the select object
• The most important event of the select object
• <Select name = "Adam" onchange = "alert ()">
• <Option value = "1" selected> 1 </option>
• <Option value = "2"> 2 </option>
• </SELECT>
• 7) add an option for select
• To add an option, you must create an option object.
• Someselect. Options [someselect. Length] = New Option (text, value );
• For example, add four options ("4", "4 ")
8) delete an option from the select object
• It is easy to delete an option, that is, to set the index option specified in the drop-down list box to null.
• Someselect. Options [1] = NULL;
• Delete the option with index 1.
• 9) Clear Select Options
• Select. Length = 0;
• 10) replace an option.
• Create a new option for the corresponding option

Framework usage skills
1. The parent Framework calls the child framework.
-Window. Frames ["framename"]
-The sub-framework in the application sub-framework can be similar:
-Window. framename. framename
2. The sub-Framework calls the parent framework
-Each window object has a parent attribute, indicating its parent framework. You can use window. Parent to represent the parent framework.
3. sibling framework reference
-Parent. Frames ["brotherframename"]
-Note that windows can be omitted.

4. Different Levels of references
-you only need to obtain the window attribute. Therefore, the framework in the framework can be called by Overlay window. framename. framename.
5. Top-level framework reference
-top attribute of window.
-If (Self = Top) // determines whether the current page is a top-level framework.

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.