SoFunction
Updated on 2025-04-14

Detailed analysis of Javascript's method of accessing html page controls page 2/2


Finally, let me talk about the usage ranges of getElementById and getElementsByName:
Id is like an ID number, it is unique, and name can have the same name just like a name.
An element defines an id. When referring to the element, the id attribute is used directly. Name is usually used in form and must be derived from .***. That is to say, the element defined by the name attribute is a child object of the document object in the script.
1. Name is used for elements in form, and is required for submission.
Id is useful for external elements of form because DOM can directly obtain a single element.
There can only be one per page
name There can be multiple names    It is not recommended to use it for some tags
3. Form elements (form input textarea select) and frame elements (iframe frame) Use name and other elements are related to form (frame elements act on the form target). On the receiving page of the form, only elements with name are received. The element assigned with ID cannot receive values ​​through the form. You can verify it yourself. There is an exception A. You can assign a name as an anchor or an ID; you can only assign an ID but not an element: (Except for elements related to the form, you can only assign an ID)   body   table  tr  td  th  p  div  span   pre  dl  dt  dd   dd   b  etc.
Here I raise another question. Since I have an ID, why do I still need a name?
The most direct answer: ID is like a person’s ID number, and name is like his name. Although ID is unique, name can be repeated.
Specifically: for ID, it is the Identity of the HTML element on the Client side. Name is actually much more complicated because Name has many uses, so it cannot be completely replaced by ID, thus canceling it.
Reference website information is as follows: The specific uses are:
Purpose 1: As a server-side label of HTML elements that can interact with the server, such as input, select, textarea, and button, etc. We can get the value submitted by the element based on its Name on the server side. Purpose   2:     HTML  Element   Input  type= "radio "   Group, we know that  radio    The control is in the same grouping class, and the check     Operation is mutex  . Only one radio    at the same time. This grouping is implemented based on the same Name   .
Purpose   3:
Purpose   4:          Identity    as an object, such as Applet  , Object  , Embed                                                                                               � For example, in the Applet object instance, we will use its Name   to reference the object.
Purpose   5:        When the IMG element and the MAP element, if you want to define the hotspot area of ​​the IMG, you need to use its attribute  usemap    to make usemap="#name" (   The associated MAP element’s Name)
Purpose   6:                                                                                                                           For example, define parameters for Object   <PARAM   NAME  =   "appletParameter"   VALUE   =   "value" > .
Obviously, these uses cannot be replaced by simply using IDs, so the ID of HTML elements and Name are not the difference between ID numbers and names. They are also different things. Of course, the Name attribute of the HTML element can also play a little ID role in the page, because in the DHTML object tree, we can use it to get an array of objects containing all the specified Name elements in the page.
By the way, what if there are n(n >1) HTML elements in the page and the IDs of all the same? How to reference them in DHTML objects? If we use ASPX page, this situation is not easy to happen, because the aspnet process does not allow IDs to be non-unique when processing aspx pages. This is when the page will be thrown and cannot be rendered normally. If it is not a dynamic page, we have to let ID repeat that IE what to do?
At this time, we can still continue to use the object, but we can only get the object that appears in HTML Render among those objects with duplicate IDs. At this time, the duplicate ID will automatically become an array when referenced, and the elements with duplicate IDs will exist in the array in order of Render.
getElementById("xxx") returns the element with the first id attribute "xxx" or the specific form element name is "xxx"
getElementsByName("xxx") returns all elements with id attribute "xxx" or the specific form element name is "xxx"
Here we want to explain that the range of elements that getElementById and getElementsByName are the same. The difference is that the former only returns the first element and the latter returns the set of all elements.
Also explain the form element. The form element refers to the tag in the <FORM> tag where data can be submitted to the server.
There are mainly three tags. <INPUT > <SELECT > <TEXTAREA > These three tags can only submit data to the server when the name attribute is not empty. So these three tags define one more name attribute. In fact, the name attribute and the id attribute are exactly the same. Both elements can be positioned.
If it is not a form element, even if you add the name attribute getElementsByName, you can't get it. If you don't believe it, try it yourself.
Due to my limited ability, I hope my friends can point out the incorrect points in a timely manner and leave a message in time for me to correct it. I am very grateful!
Previous page12Read the full text