JavaScript gets code _javascript tips for an element on a page

Source: Internet
Author: User
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>
&LT;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

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.