Ajax controls DOM nodes

Source: Internet
Author: User

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>
Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.