Getelementsbyname and getelementbyid

Source: Internet
Author: User
In the past, document. getelementbyid was often used to obtain the ID tag attribute in HTML.
Document. getelementbyname (note that there is no s) to obtain the name tag attribute. the browser reports an error. I checked the information and found that this function was not available,
It should be getelementsbyname. Unlike document. getelementbyid, the former returns a set of names and IDs, because
W3C allows duplicate name attributes, that is, the same name can be used for HTML tags, while the latter returns the first element of ID. So
Document. getelementsbyname is usually used by document. getelementsbyname ("***") [0],
[1. Document. getelementsbytagname, document. formname. Elements
The results of these methods are also set. <Input type = checkbox value = 2> 2
<Input type = checkbox value = 1> 1
<SCRIPT>
VaR arr = Document. getelementsbytagname ("input"); // get all input, and the returned result is an array.
Alert (ARR [0]. Value); // obtain the value of the first input.
</SCRIPT> here is another example: A table contains 7 or 8 tr records. I need to dynamically set 4 or 5 tr records.
Display = none/block. But I don't want to define an ID for each TR, So I thought of a common name for the four or five Tr and then use
Getelementsbyname is used to return an array. In this way, every tr convenient point is operated cyclically, but it is not supported by fa Xiang IE. If it is only an hide/show operation, you can set an independent stylesheet
Display: none. Then you can change the classname by changing the disabled property of the stylesheet.
The show/hide status of the object. <HTML>
<Head>
<Title> test </title>
</Head>
<Body>
<Style id = "ABC-style">
. ABC {display: None}
</Style>
<H1 class = 'abc'> test <SCRIPT>
Function toggle (){
VaR style = Document. getelementbyid ('abc-style ');
Style. Disabled =! Style. Disabled;
}
</SCRIPT>
<P onclick = "toggle ()"> click me </P> </body>
</Html>

Extended thinking:

For example, if you want to get the TR of "ABC" for all the class attributes, you can use this:
Code:

Alltr = Document. Evaluate ("// tr [@ class = 'abc']", document, null, xpathresult. unordered_node_snapshot_type, null );
For (VAR I = 0; I <alltr. snapshotlength; I ++ ){
Tr = alltr. snapshotitem (I );
// Do something with TR
}

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.