1: The ID is unique, and the name can be the same as the name.
2: An element defines the ID. When referencing this element, the ID attribute is directly used. Name is usually used in form and must be specified by document. form. * **, that is, the element defined by the name attribute is a sub-object of the Document Object in the script.
3: Name is used for the elements in the form, which must be submitted
ID is used for elements outside form, because Dom can directly obtain a single element.
Document. getelementbyid ("id_number") returns a single element.
Document. getelementsbyname ("name") returns an array.
4: ID, each page can have only one
Name can have multiple
Name is not recommended for some labels
Get ID: Document. getelementbyid ("idname ")
Get name: Document. getelementsbyname ("name") [0? 1? 2? 3?...]
5: Use name for form input textarea select and IFRAME Frame
These elements are related to the submission of forms (the frame element acts on the target of the form), and only
The element with name is received, and the element with ID is not received through the form. You can verify it yourself.
One exception is that a can assign name as the anchor or ID;
Only the ID can be assigned, but only the ID can be assigned to all elements related to the form)
Body Li table tr TD th P Div Span Pre dl dt dd font B and so on.
6: almost everyone who has done Web development has asked, What is the difference between the element ID and name? Why do I need a name if I have an ID? We can also get the most classical answer: ID is like a person's ID card number, and name is like his name. ID is obviously unique, and name can be repeated.
Last week, I also encountered the ID and name problems. I entered an input type = "hidden" on the page and wrote only one id = 'sliceinfo'. After the assignment, I submit it, use request. params ["sliceinfo"] does not return a value. Later, I suddenly realized that the name should be used for marking, so I added a name = 'sliceinfo' to the input, and everything was okay.
The answer to ID and name in the first section is too general. Of course, that explanation is completely correct for ID. It is the identity of the HTML element on the client side. Name is actually much more complex, because name has many purposes, so it cannot be completely replaced by ID, so it is canceled.
Specific uses include:
Purpose 1: ID of the server as an HTML element that can interact with the server, such as input, select, textarea, And button. On the server side, we can use request. Params to obtain the value submitted by the element based on its name.
Purpose 2: HTML element input type = 'radio' group. We know that the radio button control is in the same group class. The check operation is mutex and only one radio can be selected at a time, this group is implemented based on the same name attribute.
Purpose 3: Create an anchor in the page. We know that <a href = "url"> link </a> is used to obtain a page hyperlink. If you do not use the href attribute, use name instead, for example, <a name = "pagebottom"> </a>.
Purpose 4: act as the identity of an object, such as applet, object, and embed. For example, in the applet object instance, we will use its name to reference this object.
Purpose 5: When the IMG and map elements are associated, if you want to define the IMG hotspot area, you need to use its attribute usemap, make usemap = "# name" (name of the associated map element ).
Purpose 6: Attributes of certain elements, such as attribute and Param. For example, define the parameter <Param name = "appletparameter" value = "value"> for the object.
Obviously, these functions are not simply replaced by IDS, so the ID and name of the HTML element are not the same as the ID card number and name, they are actually different things.
Of course, the name attribute of the HTML element can also play a role of ID in the page, because in the DHTML Object Tree, we can use document. getelementsbyname is used to obtain an array of objects containing all the specified name elements on the page.
By the way, what if the IDs of n (n> 1) HTML elements on the page are the same? How can we reference them in DHTML objects? If we use the ASPX page, this situation is not easy, because the ASPNET process does not allow non-unique IDs when processing the ASPX page, this is because the page will be thrown an exception and cannot be a normal render. If we want to repeat the ID on a dynamic page, how does ie do it?
At this time, we can continue to use document. getelementbyid to get the object, except that we can only get the first object that appears in the HTML render of the objects with duplicate IDs. At this time, duplicate IDs are automatically converted into an array when being referenced. Elements with duplicate IDs exist in the array in order of render.