Javascript getElementById usage and usage

Source: Internet
Author: User

Document. getElementById ("link"). href;
Document. getElementById ("link" cmd.tar get;
Document. getElementById ("img"). src;
Document. getElementById ("img"). width;
Document. getElementById ("img"). height;
Document. getElementById ("input"). value;
How can we obtain the value between <div> </div> and <a> </a>? For example, aaa in <div id = "div"> aaa </div>, bbb in <a href = "#" id = "link"> bbb </a>, it is also very simple. You can use innerHTML:
Document. getElementById ("div"). innerHTML;
Document. getElementById ("link"). innerHTML;
GetElementById Method
Returns a reference to the first object with the specified ID attribute value.
Syntax
OElement = document. getElementById (sIDValue)
Parameters
SIDValue is required. String that specifies the ID Attribute Value
Return Value
Returns the first object with the same ID property value as the specified value.
Note
If the ID belongs to a set, the getElementById method returns the first object in the set.
The getElementById method is equivalent to the item method on the all set. For example, the following code sample shows how to retrieve the first element whose ID is oDiv from the document object.
Use the DHTML Object Model:
Var oVDiv = document. body. all. item ("oDiv ");
Use the Document Object Model (DOM ):
Var oVDiv = document. getElementById ("oDiv ");
Example
The following example shows how to use the getElementById method to return the first appearance of the oDiv with the ID attribute value.
<Script>
Function fnGetId (){
// Returns the first DIV element in the collection.
Var oVDiv = document. getElementById ("oDiv1 ");
}
</Script>
<Div id = "oDiv1"> Div #1 </DIV>
<Div id = "oDiv2"> Div #2 </DIV>
<Div id = "oDiv3"> Div #3 </DIV>
<Input type = "button" VALUE = "Get Names" onclick = "fnGetId ()">
GetElementById Method
Returns the first one with the specified ID attribute value. For example, the id of a text box in a webpage is called text1.
GetElementById (text1) is used to get the object of the text1 box, And all attributes and methods of the text box are used.
This is a JS method, which means to get the element value through the control ID, such as text and label in a form. They are all FORM elements and have an assigned ID, getElementById () is used to obtain the text value of these elements.
This is a JS method, which means to get the element value through the control ID, such as text and label in a form. They are all FORM elements and have an assigned ID, getElementById () is used to obtain the text value of these elements.
Program example
<Html> <pead> </pead> <body> <table width = "60%" id = "myTable" border = "1"> <tr> <td> Row 1 1 </td> <td> Row 1 cell 2 </td> </tr> <td> Row 2 cell 1 </td> <td> Row 2 cell 2 </td> </tr> <td> Row 3, cell 1 </td> <td> Row 3, cell 2 </td> </tr> </ table> </body> </ptml>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
Alignment of a detachment unit
<Html> <pead> </pead> <body> <table id = "myTable" border = "1" width = "100%"> <tr> <td> cell 1 </td> <td> Cell 2 </td> </tr> <td> cell 3 </td> <td> cell 4 </td> </tr> </table> </body> </ptml>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
Change the value of colspan
<Html> <pead> </pead> <body> <table id = "myTable" border = "1"> <tr> <td colspan = "4"> cell 1 </td> <td colspan = "4"> Cell 2 </td> </tr> <td> cell 3 </td> <td> cell 4 </ td> <td> cell 5 </td> <td> cell 6 </td> <td> cell 7 </td> <td> cell 8 </td> <td> cell 9 </td> <td> cell 10 </td> </tr> </table> </body> </ptml>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]

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.