The difference between the ID and NAME properties of the HTML element
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: Use 1: As a sign of the server side 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, Meta, and Param. For example, the parameter define parameter or meta . 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.
The name property has a problem. When we dynamically create an element that can contain the Name property, you cannot use the assignment nament.name = "..." to add its name, but you must use Document.createElement when you create Element. '
Script
Language
= "JavaScript"
>
VAR INPUT = Document.createElement ('INPUT'); Input.id = 'MyId'; Input.Name = 'MyName'; Alert (Input.outerHTML);
Script
>
The result in the message box is: .
<
Script
Language
= "JavaScript"
>
VAR INPUT = Document.createElement (' INPUT.ID =' myid '; Alert (Input.outerHTML);
Script
>
The result shown in the message box is: . This design of the initialization Name property is not the defect of IE, because MSDN said to do this, but what is the principle of this design? I didn't think too much. Here is the way, if there is N (n> 1) of the page, what should I do? How to quote them in the DHTML object? If we use the ASPX page, this is not easy because the ASPNET process is not allowed to have an ID at all when processing the ASPX page, which is a page that will be thrown and cannot be normal. If it is not a dynamic page, we have to let the ID repeat that IE how to do it? At this time, we can continue to use Document.GtelementByid to get objects, but we can only get the objects that appear in HTML Render in those objects that Id repeat. At this time, the repeated ID will automatically become an array when the reference is referenced, and the ID repeated element is in the array in the order of render.