The merging mode is formed by loading another object with an object, which is very suitable for tree structure and similar to the linked list structure.
Here is an example of my JavaScript Treeview.
VaR treebase = {
Rootnum: 0
};
Function Treeview (nodetxt, nodehref, nodetag ){
This. childnodes = [];
This.txt = nodetxt;
If (nodehref = NULL)
This. href = "";
Else
This. href = nodehref;
This.tar get = nodetag;
If (this. parentnode = NULL)
{
This. ID = "root _" + treebase. rootnum;
Treebase. rootnum = treebase. rootnum + 1;
}
}
Treeview. Prototype. tostring = function (){
VaR STR = "<div> ";
VaR c_node = this;
While (c_node.parentnode)
{
STR = STR + "1 ";
C_node = c_node.parentnode;
}
If (this. childnodes. length! = 0)
{
If (this. href. length> 0)
STR = STR + "<a href = '" + this. href + "'target = '" incluthis.tar get + "'onclick =/" javascript: showdiv (' "+ this. ID + "_ child")/">" define this.txt + "</a> ";
Else
STR = STR + "<a href =/" javascript: showdiv ('"+ this. ID +" _ child')/">" define this.txt + "</a> ";
}
Else
{
If (this. href. length> 0)
STR = STR + "<a href = '" + this. href + "'target ='" incluthis.tar get + "'>" incluthis.txt + "</a> ";
Else
Strpolicstrpolicthis.txt;
}
If (this. childnodes. length! = 0)
{
STR = STR + "<Div style =/" display: none;/"id =/" "+ this. ID +" _ child/"> ";
// Var save_pos = new array ();
// Save_pos.add (this. ID );
For (VAR int_len = 0; int_len <this. childnodes. length; int_len ++)
{
// Save_pos.push (this. ID, int_len );
STR = STR + this. childnodes [int_len];
// Int_len = save_pos.find (this. ID );
}
STR = STR + "</div> ";
}
STR = STR + "</div> ";
Return STR;
}
Function showdiv (div_id ){
VaR obj_id = Document. getelementbyid (div_id );
Obj_id.style.display = (obj_id.style.display = 'none '? 'Block': 'None ');
}
Treeview. Prototype. Add = function (node ){
Node. parentnode = this;
This. childnodes [This. childnodes. Length] = node;
If (node. childnodes. Length = 0)
{
Node. ID = This. ID + "_ child" + this. childnodes. length;
}
If(node.tar get = NULL ){
Node.targetappsthis.tar get;
}
}
Here we use parentnode to record the current object and form a dual-link structure.
When used
VaR node1 = new Treeview ("node1 ");
VaR node2 = new Treeview ("node2 ");
Node1.add (node2 );
Document. Write (node1 );
In this way, you can