JavaScript DOM programming--04--getting text nodes

Source: Internet
Author: User

Get text node:
1). Step: Element node--Get child node of element node
2). If the element node has only one child node of the text node,
For example <li id= "BJ" name= "Beijing" > Beijing </li>, <p> which city do you like? </p>
You can get to the specified element node Elenode first,
Then use the EleNode.firstChild.nodeValue method to read and write the value of its text node

1 <HTML>2     <Head>3         <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8">4         <title>Untitled Document</title>5         6         <Scripttype= "Text/javascript">7             8             //Get text node9 window.onload= function(){Ten                  One                 //the text node must be a child node of the element node. A                  -                 //1. Get the element node where the text node resides -                 varBjnode=document.getElementById ("BJ"); the                  -                 //2. Defined by FirstChild to a text node -                 varBjtextnode=Bjnode.firstchild; -                  +                 //3. Read and write the value of the text node by manipulating the NodeValue property of the text node. - alert (bjtextnode.nodevalue); +                  A Bjtextnode.nodevalue= "still Silicon Valley"; at                  -                 //alert (bjtextnode); -             } -              -         </Script> -          in     </Head> -     <Body> to         <P>Which city do you like?</P> +         <ulID= "City"> -             <LiID= "BJ"name= "Beijing">Beijing</Li> the             <Li>Shanghai</Li> *             <Li>Tokyo</Li> $             <Li>Seoul</Li>Panax Notoginseng         </ul> -          the         <BR><BR> +         <P>Which stand-alone game do you like?</P> A         <ulID= "Game"> the             <LiID= "RL">Red Police</Li> +             <Li>Live</Li> -             <Li>Need for Speed</Li> $             <Li>Warcraft</Li> $         </ul> -          -         <BR><BR> theName<inputtype= "text"name= "username"  - ID= "Name"value= "Atguigu"/>Wuyi     </Body> the </HTML>    

JavaScript DOM programming--04--getting text nodes

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.