JavaScript gets the sample code for the specified object on the current page _javascript tips

Source: Internet
Author: User
Tags tag name
How JavaScript gets the specified object on the current page.

The method is as follows:
Copy Code code as follows:

document.getElementById (ID)//Get object with specified ID value
Document.getelementsbyname (name)//Get an array of objects with the specified Name value
Document.all[]//very smart stuff but not web standards
document.getElementsByTagName//Get an array of objects with the specified label value

Here is an example, you can see the effect by simply removing the annotation directly from the operation.
Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<meta name= "generator" content= "EditPlus" >
<meta name= "Author" content= "" >
<meta name= "Keywords" content= "" >
<meta name= "Description" content= "" >
</HEAD>

<BODY>
<form method= "POST" name= "mainfrm" action= "" >
<input type= "hidden" name= "text" id= "text" style= "width:100%" value= "Practice only" >
<input type= "hidden" name= "OrganizationId" style= "width:100%" value= "Authentication document.all" >
<table width= "100%" border= "1" >
<tr height= ">"
&LT;TD width= "15%" align= "right" > Inventory Organization:</td>
&LT;TD width= "20%" ><input type= "text" name= "OrganizationId" id= "OrganizationId" "style=" width:100% "value=" inventory organization "></td>
</tr>
<tr>
&LT;TD width= "15%" align= "right" > sub-Library code:</td>
&LT;TD width= "20%" >
<select name= "Subinventorycode" style= "width:100%" id= "Subinventorycode" >
<option value= "QTWL" >QTWL</option>
<option value= "BTSPT" >BTSPT</option>
<option value= "BTS" >BTS</option>
</select>
</td>
</tr>
<tr height= ">"
&LT;TD colspan= "2" align= "center" ><input type= "button" value= "Get the specified object" onclick= "Do_check ()" ></td>
</tr>
</table>
</form>
</BODY>
</HTML>
<script language= "JavaScript" >
<!--
function Do_check () {
getElementById: The element is obtained by ID, so only the element with the ID set is accessible.
The return value of the method is guaranteed to be the object you need, because the ID value of an object in the entire page is unique.
var OrganizationId = document.getElementById ("OrganizationId");
alert (Organizationid.value);


Getelementsbyname: The element is obtained by name.
The return value of the method is an array, and it is returned as an array even if there is only one object in the entire page that has a name property of the given value.
Only then the length of the array is one.
var OrganizationId = document.getelementsbyname ("OrganizationId");
alert (Organizationid[0].value);
alert (organizationid.length);

getElementsByTagName: by tagname (tag name) to get the elements, a document will of course have the same label,
So the basic acquaintance of this method and Getelementsbyname is also to get an array, just to get the difference in the way of the object.
var inputs = document.getElementsByTagName ("input");
alert (inputs.length);
alert (Inputs[0].value);
alert (Inputs[1].value);
alert (Inputs[2].value);

Document.all[] is an array variable of all the labels in the document, including all the elements in the Document object.
Usually gets the specified element by name, but it's smarter than getelementsbyname. If one of the eligible objects returns this object, multiple returns in the form of an array
var OrganizationId = document.all["OrganizationId"];
Alert (Organizationid[0].value)
document.all["OrganizationId"]
Document.All.Item ("OrganizationId")
}
-->
</SCRIPT>
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.