To create elements dynamically:
Document.createelement (' element name ');
To delete an element:
Parent. RemoveChild (the element to be deleted);
To add an element:
Parent. appendchild (the element to be added)----> Add to the back;
The parent. InsertBefore (the element to add, the front of whom to add)----> Add to the front of an element;
AppendChild () and insertbefore () are similar to the function of shearing;
Send text:
Window.onload=function () {
var Otxt=document.getelementbyid ("txt");
var Obtn=document.getelementbyid ("btn");
var Obox=document.getelementbyid ("box")
Obtn.onclick=function () {//button click Add New Li;
Sendmess ();
}
Otxt.onkeydown=function (EV) {//otxt, keyboard press to add a new Li;
var oev=ev| | Event
if (oev.keycode==13) {//enter key
Sendmess ();
}
}
function sendmess () {
var ali=document.createelement (' Li '); Create a new element;
Ali.innerhtml=otxt.value+ ' <a href= "javascript:;" > Delete </a> ';
if (oBox.children.length) {//obox.children.length is true, execute if
Obox.insertbefore (Ali,obox.children[0]);
}
Else{obox.appendchild (aLi);}
Otxt.value= ";
Ali.children[0].onclick=function () {//a Click Remove entire LI from UL;
Obox.removechild (This.parentnode);
}
}
}
Creating elements dynamically