当前位置:Gxlcms > JavaScript > 使用Javascript和DOMInterfaces来处理HTML_javascript技巧

使用Javascript和DOMInterfaces来处理HTML_javascript技巧

时间:2021-07-01 10:21:17 帮助过:13人阅读

1、创建表格

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]

Note the order in which we created the elements and the text node:

First we created the TABLE element.
Next, we created the TBODY element, which is a child of the TABLE element.
Next, we used a loop to create the TR elements, which are children of the TBODY element.
For each TR element, we used a loop to create the TD elements, which are children of TR elements.
For each TD element, we then created the text node with the table cell's text.
Once we have created the TABLE, TBODY, TR, and TD elements and then the text node, we then append each object to its parent in the opposite order:



First, we attach each text node to its parent TD element using
mycurrent_cell.appendChild(currenttext);
Next, we attach each TD element to its parent TR element using
mycurrent_row.appendChild(mycurrent_cell);
Next, we attach each TR element to the parent TBODY element using
mytablebody.appendChild(mycurrent_row);
Next, we attach the TBODY element to its parent TABLE element using
mytable.appendChild(mytablebody);
Next, we attach the TABLE element to its parent BODY element using
mybody.appendChild(mytable);


Remember this technique. You will use it frequently in programming for the W3C DOM. First, you create elements from the top down; then you attach the children to the parents from the bottom up.

Here's the HTML markup generated by the JavaScript code:

...



cell is row 0 column 0cell is row 0 column 1
cell is row 1 column 0cell is row 1 column 1

...

Here's the DOM object tree generated by the code for the TABLE element and its child elements:

Image:sample1-tabledom.jpg

You can build this table and its internal child elements by using just a few DOM methods. Remember to keep in mind the tree model for the structures you are planning to create; this will make it easier to write the necessary code. In the TABLE tree of Figure 1 the element TABLE has one child, the element TBODY. TBODY has two children. Each TBODY's child (TR) has one child (TD). Finally, each TD has one child, a text node.


2、

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]

In this example, we set the myP variable to the DOM object for the second p element inside the body:



First, we get a list of all the body elements via
document.getElementsByTagName("body")
Since there is only one body element in any valid HTML document, this list will have only one item.
Next, we get the first element on that list, which will be the object for the body element itself, via
myBody=myDocumentElements.item(0);
Next, we get all the p elements that are children of the body via
myBodyElements=myBody.getElementsByTagName("p");
Finally, we get the second item from the list of p elements via
myP=myBodyElements.item(1);


Image:sample2a2.jpg

Once you have gotten the DOM object for an HTML element, you can set its properties. For example, if you want to set the style background color property, you just add:

myP.style.background="rgb(255,0,0)";
// setting inline STYLE attribute


[编辑]

Creating TextNodes with document.createTextNode(..)
Use the document object to invoke the createTextNode method and create your text node. You just need to pass the text content. The return value is an object that represents the text node.

myTextNode=document.createTextNode("world");

This means that you have created a node of the type TEXT_NODE (a piece of text) whose text data is "world", and myTextNode is your reference to this node object. To insert this text into your HTML page, you need to make this text node a child of some other node element.



[编辑]

Inserting Elements with appendChild(..)
So, by calling myP.appendChild([node_element]), you are making the element a new child of the second P element.

myP.appendChild(myTextNode);

After testing this sample, note that the words hello and world are together: helloworld. So visually, when you see the HTML page it seems like the two text nodes hello and world are a single node, but remember that in the document model, there are two nodes. The second node is a new node of type TEXT_NODE, and it is the second child of the second P tag. The following figure shows the recently created Text Node object inside the document tree.

Image:sample2b2.jpg

createTextNode and appendChild is a simple way to include white space between the words hello and world. Another important note is that the appendChild method will append the child after the last child, just like the word world has been added after the word hello. So if you want to append a Text Node between hello and world you will need to use insertBefore instead of appendChild.


[编辑]

Creating New Elements with the document object and the createElement(..) method
You can create new HTML elements or any other element you want with createElement. For example, if you want to create a new P element as a child of the BODY element, you can use the myBody in the previous example and append a new element node. To create a node simply call document.createElement("tagname"). For example:

myNewPTAGnode=document.createElement("p");
myBody.appendChild(myNewPTAGnode);

Image:sample2c.jpg



[编辑]

Removing nodes with the removeChild(..) method
Each node can be removed. The following line removes the text node which contains the word world of the myP (second P element).

myP.removeChild(myTextNode);

Finally you can add myTextNode (which contains the word world) into the recently created P element:

myNewPTAGnode.appendChild(myTextNode);

The final state for the modified object tree looks like this:

Image:sample2d.jpg



[编辑]

Creating a table dynamically (back to Sample1.html)
For the rest of this article we will continue working with sample1.html. The following figure shows the table object tree structure for the table created in the sample.



[编辑]

Reviewing the HTML Table structure
Image:sample1-tabledom.jpg



[编辑]

Creating element nodes and inserting them into the document tree
The basic steps to create the table in sample1.html are:

Get the body object (first item of the document object).
Create all the elements.
Finally, append each child according to the table structure (as in the above figure). The following source code is a commented version for the sample1.html.
At the end of the start function there is a new line of code. The table's border property was set using another DOM method, setAttribute. setAttribute has two arguments: the attribute name and the attribute value. You can set any attribute of any element using the setAttribute method.

Sample code - Traversing an HTML Table with JavaScript and DOM Interfaces