<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "Default. aspx. cs" Inherits = "_ Default" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> DOM methods and attributes </title>
<Script language = "javascript">
Function AddElement ()
{
Var para = document. createElement ("p ");
Para. setAttribute ("title", "My paragraph ");
Var message = document. createTextNode ("hello world ");
Para. appendChild (message );
Document. getElementById ("container"). appendChild (para );
// Alert (para. nodeName); // P
// Alert (message. nodeName); // # text
// Alert (para. nodeType); // 1
// Alert (message. nodeType); // 3
// Var value = para. getAttribute ("title ");
// Alert (value );
}
Function CloneNode ()
{
Var para = document. createElement ("p ");
Para. setAttribute ("id", "p1 ");
Para. setAttribute ("title", "My paragraph ");
Var message = document. createTextNode ("hello world ");
Para. appendChild (message );
Document. getElementById ("container"). appendChild (para );
Var newpara = para. cloneNode (true );
// Alert (newpara. getAttribute ("id "));
Newpara. setAttribute ("id", "p2 ");
// Alert (newpara. childNodes [0]. nodeValue );
Document. getElementById ("container"). appendChild (newpara );
}
Function Analyse ()
{
Var sel = document. getElementById ("sel2 ");
Alert (sel. childNodes. length); // 6
Var NodesArray = sel. childNodes;
For (var I = 0; I <NodesArray. length; I ++)
{
// Alert (sel. childNodes [I]. nodeName );
If (I % 2! = 0) // 1 3 5
{
// Var _ value = sel. childNodes [I]. childNodes [0]. nodeValue;
// Var _ value = sel. childNodes [I]. getAttribute ("value ");
// Var _ value = NodesArray [I]. childNodes [0]. nodeValue;
Var _ value = NodesArray. item (I). childNodes [0]. nodeValue;
Alert (_ value );
}
Else
{
// Var value = sel. childNodes [I]. nodeValue;
// Alert (value );
}
}
}
Function RemoveChild ()
{
Var sel2 = document. getElementById ("sel2 ");
If (sel2.hasChildNodes)
{
Var firstoption = sel2.childNodes [1];
If (firstoption)
{
Var removednode = firstoption. parentNode. removeChild (firstoption );
Alert (removednode. childNodes [0]. nodeValue );
}
}
}
Function ReplaceChild ()
{
Var sel2 = document. getElementById ("sel2 ");
Var newoption = document. createElement ("option ");
Newoption. setAttribute ("value", "4 ");
Var text = document. createTextNode ("4 ");
Newoption. appendChild (text );
Var oldoption = sel2.replaceChild (newoption, sel2.childNodes [1]); // return oldoption
// Alert (oldoption. getAttribute ("value "));
}
Function CycleSelect1 () // traverse the drop-down list. Each option starts from the first one.
{
Var sel2 = document. getElementById ("sel2 ");
// Alert (sel2.tagName );
Var tempChild = null;
TempChild = sel2.firstChild;
While (tempChild)
{
If (tempChild. tagName = "OPTION ")
{
// Alert (tempChild. tagName );
Alert (tempChild. getAttribute ("value "));
}
TempChild = tempChild. nextSibling;
}
}
Function CycleSelect2 () // traverse the drop-down list. Each option starts from the last one.
{
Var sel2 = document. getElementById ("sel2 ");
// Alert (sel2.tagName );
Var tempChild = null;
TempChild = sel2.lastChild;
While (tempChild)
{
If (tempChild. tagName = "OPTION ")
{
// Alert (tempChild. tagName); // OPTION
// Alert (tempChild. nodeName); // OPTION
Alert (tempChild. getAttribute ("value "));
}
TempChild = tempChild. previussibling;
}
}
Function GetElementsByTagName ()
{
Var sel2 = document. getElementById ("sel2 ");
Var _ array = sel2.getElementsByTagName ("OPTION ");
For (var I = 0; I <_ array. length; I ++)
{
Var _ value = _ array. item (I). childNodes [0]. nodeValue;
Alert (_ value );
}
}
Function appendChildCutPaste () // cut and paste
{
Var sel2 = document. getElementById ("sel2 ");
Var child1 = sel2.getElementsByTagName ("OPTION"). item (0 );
Sel2.appendChild (child1 );
}
Function InsertBefore ()
{
Var sel2 = document. getElementById ("sel2 ");
Var _ text = sel2.options. length. toString ();
Var newoption = document. createElement ("option ");
Newoption. setAttribute ("value", _ text );
Var text = document. createTextNode (_ text );
Newoption. appendChild (text );
Sel2.insertBefore (newoption, sel2.lastChild );
}
Function InsertBeforeCutPaste ()
{
Var sel2 = document. getElementById ("sel2 ");
Sel2.insertBefore (sel2.options [0], sel2.lastChild );
}
Function insertAfter (newElement, targetElement) // insert a given node to the end of the subnode
{
Var parent = targetElement. parentNode;
If (parent. lastChild = targetElement)
{
Parent. appendChild (newElement );
}
Else
{
Parent. insertBefore (newElement, targetElement. nextSibling );
}
}
Function InsertAfter ()
{
Var sel2 = document. getElementById ("sel2 ");
Var _ text = sel2.options. length. toString ();
Var newoption = document. createElement ("option ");
Newoption. setAttribute ("value", _ text );
Var text = document. createTextNode (_ text );
Newoption. appendChild (text );
InsertAfter (newoption, sel2.lastChild );
}
Function InsertAfterCutPaste ()
{
Var sel2 = document. getElementById ("sel2 ");
InsertAfter (sel2.options [0], sel2.lastChild );
// InsertAfter (sel2.options [0], sel2.options [2]);
}
</Script>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Input type = "button" value = "Submit" onclick = "AddElement ();"/>
<Input type = "button" value = "CloneNode" onclick = "CloneNode ();"/>
<Div id = "container">
</Div>
<Hr/>
<Select id = "sel1" multiple size = 1>
<Option selected value = 1> 1 </option>
<Option value = 2> 2 </option>
<Option value = 3> 3 </option>
</Select>
<Hr/>
<Select id = "sel2" size = "1">
<Option selected value = 0> 0 </option>
<Option value = 1> 1 </option>
<Option value = 2> 2 </option>
</Select> <br/>
<Input type = "button" value = "analysis" onclick = "Analyse ();"/>
<Input type = "button" value = "RemoveChild" onclick = "RemoveChild ();"/>
<Input type = "button" value = "ReplaceChild" onclick = "ReplaceChild ();"/>
<Input type = "button" value = "traverse the drop-down list. Each option starts from the first option." onclick = "CycleSelect1 ();"/>
<Input type = "button" value = "traverse the drop-down list. Each option starts from the last one." onclick = "CycleSelect2 ();"/>
<Input type = "button" value = "GetElementsByTagName" onclick = "GetElementsByTagName ();"/>
<Br/>
<Input type = "button" value = "appendChild cut and paste" onclick = "appendChildCutPaste ();"/>
<Input type = "button" value = "insertBefore" onclick = "InsertBefore ();"/>
<Input type = "button" value = "insertBefore cut and paste" onclick = "InsertBeforeCutPaste ();"/>
<Input type = "button" value = "insertAfter" onclick = "InsertAfter ();"/>
<Input type = "button" value = "insertAfter cut and paste" onclick = "InsertAfterCutPaste ();"/>
</Form>
</Body>
</Html>