How to get the element node:
1). document.getElementById: Gets the corresponding individual node based on the id attribute
2). document.getElementsByTagName:
Gets an array of the specified node names based on the tag name, the length property of the array object can get the lengths of the array
3). Document.getelementsbyname:
Gets an array of nodes that match the criteria based on the Name property of the node.
However, the way IE is implemented differs from the standard:
In an HTML document, if a node (LI) does not have a name attribute,
Then IE uses getelementsbyname cannot get to the node array, but Firefox can.
4). Other two methods, IE is not supported at all, so it is not recommended to use
2 <HTML>3 <Head>4 <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8">5 <title>Untitled Document</title>6 7 <Scripttype= "Text/javascript">8 9 //gets the specified element node.Ten window.onload= function(){ One A //1. Get the node whose ID is BJ. - //when you write an HTML document, make sure that the id attribute value is unique. - //This method is the method of the Document object the varBjnode=document.getElementById ("BJ"); - alert (bjnode); - - //2. Get all the LI nodes. + //gets the collection of specified nodes using the tag name. - //This method is the method of node interface, that is, any node has this method. + varLinodes=document.getElementsByTagName ("Li"); A alert (linodes.length);
atGets the li of the specified node - varCitynode=document.getElementById (" City"); - varCitylinodes=Citynode.getelementsbytagname ("Li"); - alert (citylinodes.length); - - //3. Gets a collection of the specified nodes based on the Name property names of the HTML document elements. in varGendernodes=Document.getelementsbyname ("Gender"); - alert (gendernodes.length); to + //if the HTML element itself does not have a name attribute defined, then Getelementsbyname () - //method is not valid for IE. So be cautious when using this method . the varBjNode2=Document.getelementsbyname ("Beijing"); * alert (bjnode2.length); $ Panax Notoginseng } - the </Script> + A </Head> the <Body> + <P>Which city do you like?</P> - <ulID= "City"> $ <LiID= "BJ"name= "Beijing">Beijing</Li> $ <Li>Shanghai</Li> - <Li>Tokyo</Li> - <Li>Seoul</Li> the </ul> - Wuyi <BR><BR> the <P>Which stand-alone game do you like?</P> - <ulID= "Game"> Wu <LiID= "RL">Red Police</Li> - <Li>Live</Li> About <Li>Need for Speed</Li> $ <Li>Warcraft</Li> - </ul> - - <BR><BR> A Gender: + <inputtype= "Radio"name= "Gender"value= "Male"/>Male the <inputtype= "Radio"name= "Gender"value= "female"/>Female - </Body> $ </HTML>
JavaScript dom programming--02--getting ELEMENT nodes