Dom Create and DELETE nodes, HTML dom common objects [goto]

Source: Internet
Author: User

Create and DELETE nodes:--core DOM
1. Create a single element node: 3 steps:
1. Create an empty element node object:
var elem=document.createelement ("tag name");
For example: Var a=document.createelement ("a");
HTML: <a></a>
2. Set the key properties of the new element:
For example: a.href= "http://tmooc.cn";
A.innerhtml= "Go to Tmooc";
HTML: <a href= "http://tmooc.cn" >go to Tmooc</a>
3. Hang the new node under the specified parent node of the DOM tree: 3 Kinds:
Added: Parent.appendchild (elem);
Insertion: Parent.insertbefore (Elem,oldelem);
Replacement: Parent.replacechild (Elem.oldelem);
2. Create multiple lateral ELEMENT nodes: 3 steps:
1. Create a document fragment object first:
var frag=document.createdocumentfragment ();
Document fragment: An object in memory that temporarily holds a DOM subtree
When to use: whenever you add more than one peer, you first place the document fragment, and then the entire document fragment is attached to the DOM tree
2. Place the child node first in the document fragment
3. Attach the document fragment to the DOM tree as a whole
The document fragment itself does not appear on the DOM tree, only the elements are added to the DOM trees

3. Delete: Parent.removechild (child node);
Child nodes. Parentnode.removechild (child nodes);

2. HTML Dom Common objects:
1. Select object:
Event: onchange When the selected item has changed
Properties: Sel.selectedindex Gets the position of the currently selected item
Sel.options gets all option element objects under SEL
Method: Sel.add (opt) append opt to Sel
Sel.remove (i) delete the option element under the SEL i position
Option object:
Create a new Option:var opt=new Option (text,value);
Equivalent to the core Dom three sentence:
var opt=document.createelement ("option");
Opt.value=value;
Opt.innerhtml=text;
Properties: Opt.index Gets the position subscript of the current opt location
Opt.text opt.value: Get innerHTML and value respectively
Opt.selected: Determine if the current opt is selected!
Emphasis: Opt's selected is not a bool value!
But it can be judged directly as a condition.

Create option and append to select:
Sel.add (New Option (Text,value));

Dom Create and DELETE nodes, HTML dom common objects [goto]

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.