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]