The difference between the Id and Name properties of the HTML element [repost]

xiaoxiao2021-03-06  36

It can be said that almost every people who have done Web have asked. What is the difference between the ID and Name of the element? Why do you have an ID to have Name ?! And we can also get the most Classical answer: ID is like a person's ID number, and Name is like his name, ID is clearly unique, and Name It can be repeated.

Last week I also encountered ID and name questions, I entered an input type = "hidden" in the page, I only wrote an id = 'sliceInfo', and Submit after assignment, in the background with Request.Params ["sliceInfo"] But how can I get a value. Later, I suddenly realized that it was indicated by name, so I added a name = 'sliceinfo' in Input, everything OK.

The first paragraph is too general for ID and name, of course, the explanation is completely correct for the ID, it is the Identity of the Client End HTML element. Name is actually more complicated, because Name has many uses, so it does not completely replaced by IDs, thereby canceling it. Specific uses are:

Uses 1: As the server end of the HTML element that can interact with the server, such as INPUT, SELECT, TEXTAREA, and Button, etc. We can get the value submitted by the Element by Request.Params in the server side. Upu 2: HTML Element INPUT TYPE = 'Radio' Packet, we know that the Radio button control is in the same packet class, the check operation is MUTEX. On the same time, only one Radio can be selected, which is implemented according to the same name attribute. Uses 3: Create an anchor in the page, we know link is a page hyperlink, if you do not need href attribute, use name, such as we gain a page anchor point. Uses 4: As an object of Identity, such as applet, object, embed and other elements. For example, in the Applet object instance, we will use its Name to reference the object. Use 5: When the IMG element and the MAP element are associated, if you want to define the hotspot area of ​​the IMG, you need to use its properties UseMap so that useMAP = "# name" (Name of the associated MAP element). Uses 6: Attributes of certain specific elements, such as Attribute, and Param. For example, parameter is defined for Object.

Obviously these uses are not easy to use ID, so the ID and NAME of the HTML element are not the difference between the ID number and the name, and they are more different.

Of course, the Name property of the HTML element can also serve as a bit of ID, because in the DHTML object tree, we can use Document.getElementsByName to get an object array of objects containing all specified NAME elements in the page.

转载请注明原文地址:https://www.9cbs.com/read-61793.html

New Post(0)