I have read an article about how to use ajax to operate DOM nodes. In fact, I am not quite clear about it. He provides an example of the source code, but it cannot be solved, after studying it for half a day, I finally modified a usable one. Let's take a look at it. It's easy to capture the first two HTML nodes and delete an image node.
<HTML>
<Head>
<Title> JavaScript and the DOM </title>
<Script language = "JavaScript">...
Function Test ()...{
VaR mydocument = document;
VaR htmlelement = mydocument.doc umentelement;
Alert ("the root element of the page is" + htmlelement. nodename );
VaR headelement = htmlelement. getelementsbytagname ("head") [0];
If (headelement! = NULL )...{
Alert ("we found the head element, named" + headelement. nodename );
VaR titleelement = headelement. getelementsbytagname ("title") [0];
If (titleelement! = NULL )...{
VaR titletext = titleelement. firstchild;
// Alert ("the page title is" + titletext. nodename );
}
VaR bodyelement = headelement. nextsibling;
While (bodyelement. nodename. tolowercase ()! = "Body ")...{
Bodyelement = bodyelement. nextsibling;
}
If (bodyelement. haschildnodes ())...{
For (I = 0; I <bodyelement. childnodes. length; I ++ )...{
VaR currentnode = bodyelement. childnodes [I];
If (currentnode. nodename. tolowercase () = "IMG ")
...{
Bodyelement. removechild (currentnode );
}
}
}
}
}
</SCRIPT>
</Head>
<Body>
<P> JavaScript and Dom are a perfect match.
You can read more in <I> head rush Ajax </I>. </P>
<Input type = "button" value = "test me! "Onclick =" test (); "/>
</Body>
</Html>