GetelementbynameDoes not exist
OnlyGetelement _ S _ bynameAndGetelementbyid
ByidAccordingHtmlElementIDAttribute, according to specificationsIDIt should be unique and used to indicateElementFor example<TD>, <div> ....... Therefore, the unique element handle orNull. For Form Controls<Input> <SELECT> <textarea>When not specifiedIDWhen,NameIt will also be consideredIDFor example<Input type = "text" name = "ABC" value = "123">You can also useDocument. getelementbyidx ("ABC ")To obtain
WhileBynameFrom the name, you can know that it returns multiple objects. It is based onNameProperty (NameCan be repeated) to get allNameAn array composed of controls with the same properties and parameters. If the control does not exist0Length Array(. Length = 0,InsteadNull)
For example
<Input type = "text" name = "ABC" value = "1">
<Input type = "text" name = "ABC" value = "2">
<Input type = "text" name = "ABC" value = "3">
VaR array = Document. getelementsbyname ("ABC ");
If (array. Length = 0 ){
Alert ("error ");
Return;
}
For (VAR I = 0; I <array. length; I ++ ){
Alert (array [I]. value );
}
When there are multiple widgets with the same name on the page , What do you do first ? Judge Length ? Indeed , SlaveProgramFrom a strict perspective , We need to determine the length. , There are two reference methods: length and no length. . Let's see :
Oele = Document. All. AAA ;// Here is Aaa Object , But we don't know how long it is. , So there is no way to operate on it . Therefore , We need to judge the length first. . As follows: :
If (oele. Length) {} else {};
In either case , The content in curly braces is written differently. :
If (oele. Length ){
For (VAR I = 0; I <oele. length; I ++ ){
Oele [I]. value ........
}
}
Else {
Oele. value ........
};
But is writing like this too complicated??And whenCodeWhen there are many,We need to write the code twice.,Dizzy first~
FortunatelyDocument. getelementsbyname ()This method.It is the same for one or more operations.,We can use:
Oele = Document. getelementsbyname ('aaa ')To reference
WhenOeleOnly1Time (s),That isOele [0],When there are multiple,Subscript MethodOele [I]Loop acquisition,Is it easy??
It is worth mentioning that Name And ID Is equally valid .
But it can only be applied Document Object . Corresponding , There is another way , Objects that can be applied will be wider. :
Getelementsbytagname, For example <Div id = 'aaa'> <input/> ...... </div>
I want to fetch Div All Input, You can write it in this way. : AAA. getelementsbytagname_r ('input '), In this way, it works with other Div ( For example Bbb Of Div, The same is true. Input) Differences .
Same Getelementsbytagname Counterpart , There is another Document. Body. All. Tags (), Object ratio using this method Getelementsbytagname Much smaller . However Getelementsbyname More .
Here we will also mention Getelementbyid, It also has only Document Objects can be used , The first element of the array is returned. , Haha , Its method names are clearly written Getelement Instead Getelements, So , Never be confused. .