ID and name
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 side of the HTM l 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.
Class and ID
A class is used to define one or more elements according to user-defined standards. For example, a script: A class can define the story line of each character in the script. You can use this class through CSS and JavaScript. Therefore, you can use class = "Frodo", class = "gandalf", class = "Aragorn" on a page to differentiate different story lines. Another important thing is that you can use any number of classes in a document.
ID is usually used to define a tag that appears only once on the page. In structured layout of page layout (for example, a page is usually composed of a header and a header , A content area and a footer). Generally, the ID is ideal, because an ID can only be used once in a document. These elements are rarely larger than once on the same page.
In one sentence, the class can be used repeatedly, and the ID can only be used once on a page. It is possible that the same ID will not be used repeatedly in many browsers, but it is definitely incorrect in terms of standards, and it may lead to practical problems in some browsers. (I tried it in Dreamweaver just now. Indeed, there is no error)
In practical application, the class may be useful for text layout, and the ID is useful for macro layout and design of various elements.
Funny example:
Simple analogy 1
A school (like a page)
Class 2 and class 3 (like a block, it also happens to have class = "S23 ")
Student Popo (like an object, id = "Popo ")
White shirt (like a Class, Class = "whiteshirt ")
There can be many students wearing white shirts in the school (for example, there can be many identical classes on the page)
You (like a function () function in Javascript)
Go to school to find Popo (the target is already found)
If you tell your teacher to find a student wearing a white shirt, the teacher will give you a bunch of students wearing a white shirt.
So you must tell the teacher to find a student named Popo.
Simple analogy 2
ID must be unique and should be used in the periphery whenever possible. The class is reproducible and should be used within the structure as much as possible. The advantage of doing so is that it is beneficial to the websiteCodeLater maintenance and modification, this approach will make all classes become sub-or grandchildren of IDs.You can have two sons, but can you have two fathers?