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
}