GetAttribute and setAttribute get and set object attributes: functiondocumenttest({{varohtmldocument.doc umentElement; // get & lt; html/& gt; element varoHeadoHtml. firstChild; // get & lt; head/& gt; varoBod getAttribute and setAttribute get and set object attributes:
Function documentTest (){
Var oHtml = document.doc umentElement; // getElement
Var oHead = oHtml. firstChild; // get
Var oBody = oHtml. lastChild; // get
OBody. setAttribute ("id", "bodyId ");
Alert (oBody. getAttribute ("id "));
}
Create HTML elements:
// Create
Function createNode (){
Var oP = document. createElement ("p"); // create a p tag
Var sText = document. createTextNode (""); // create a text node that contains the text ""
OP. appendChild (sText); // Add sText to the end of the oP node.
Document. body. appendChild (oP );
}
Remove:
// Remove
Function deleteNode (){
Var oP = document. body. getElementsByTagName ("p") [0];
/* Document. body. removeChild (oP );*/
OP. parentNode. removeChild (oP );
}
Replace:
// Replace
Function replaceP (){
Var oNewOp = document. createElement ("p ");
Var sNewText = document. createTextNode ("enchanting ");
ONewOp. appendChild (sNewText );
Var oP = document. body. getElementsByTagName ("p") [0];
OP. parentNode. replaceChild (oNewOp, oP );
}
Make the new message appear before the old message:
Function insertNode (){
Var oP = document. createElement ("p ");
Var sText = document. createTextNode ("Heartbroken ");
OP. appendChild (sText );
Var ooP = document. body. getElementsByTagName ("p") [0];
/**
* Two parameters are received, that is, the node to be inserted, before which node is inserted.
*/
Document. body. insertBefore (oP, ooP );
}
CreateDocumentFragment () file fragmentation: mentioned in javaScript Optimization
/**
* Document fragments
* Once a node is added to document. body (or its descendant node), the page updates and reflects this change,
* This is good for a small number of updates, as in the previous example. However, when you want to add a large amount of data to the document,
* If you add these changes one by one, the process may be very slow. To solve this problem, you can create a document fragment,
* Add all new nodes to the file, and add the content of the file fragments to the document at one time.
* The sixth and sixth points of javaScript optimization are also mentioned.
*/
Function createDocument (){
Var array = ["undefeated in the East", "Ling huchong", "Renren", "Wang Yu", "zhu ", "Azi", "cat", "dog", "Jing brother"];
Var oF = document. createDocumentFragment ();
For (var I = 0; I var oP = document. createElement ("p ");
Var sText = document. createTextNode (array [I]);
OP. appendChild (sText );
OF. appendChild (oP );
}
Document. body. appendChild ();
}
Two ways to operate a table
Function createTable (){
Var oTable = document. createElement ("table ");
OTable. setAttribute ("border", "1 ");
OTable. setAttribute ("width", "100% ");
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.