The syntax recommended by the consortium is to obtain an element within the DOM tree through the document object, common methods getElementById (), Getelementsbyname (), getElementsByTagName (), Their role is to return a single node or collection of nodes by id attribute, name attribute/tag name, respectively.
Copy Code code as follows:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>javascript how to get an element on a page </title>
<script type= "Text/javascript" >
function Dogetelementsbytagname () {
var x=document.getelementsbytagname ("input");
alert (x.length);
}
function Dogetelementsbyname () {
var x=document.getelementsbyname ("Myinput");
alert (x.length);
}
function GetValue () {
var X=document.getelementbyid ("MyHeader");
alert (x.innerhtml);
}
</script>
<body>
<H1 id= "MyHeader" onclick= "GetValue ();" > here is Head <p> click on the header pop-up MyHeader content </p>
<input type= "text" name= "Myinput" size= ""/>
<br/>
<br/>
<input type= "text" name= "Myinput" size= ""/>
<br/>
<br/>
<input type= "text" name= "Myinput" size= ""/>
<br/>
<br/>
<input type= "button" onclick= "Dogetelementsbytagname (); How many input elements does" value= "have? "/>
<br/>
<input type= "button" onclick= "Dogetelementsbyname ();" value= "How many name values are myinput? "/>
</body>
When clicked:, Eject:
When clicked:, Eject:
When clicked:, Eject:
Especially in the following code,
Copy Code code as follows:
function GetValue () {
var X=document.getelementbyid ("MyHeader");
alert (x.innerhtml);
}
Involves a knowledge point: HTML DOM InnerHTML, which is the entire contents of the object from the start position to the ending position, including the HTML tag