Difference between ID and name in HTML
A name can correspond to multiple controls at the same time, such as checkbox and radio.
The ID must be unique in the entire document.
Purpose of ID
1) ID is the identity of HTML elements, mainly used in client scripts.
2) label and Form Control Association, as shown in figure
<Label for = "myinput"> my input </label>
<Input id = "myinput" type = "text">
The for Attribute specifies the ID of the element associated with the label and cannot be replaced by name.
3) Get objects in the script: IE supports directly referencing the objects identified by this ID with ID (rather than name) in the script.
For example, to obtain the input content in the script, you can directly obtain the input content using myinput. value. If Dom is used, document. getelementbyid ("myinput"). value;
If you want to use name, you usually first obtain the form containing the control, such as document. forms [0], and then reference the name from form. Note that the calculated value will be sent to the server.
Usage of name
1) it is mainly used to obtain information about a form field of the submitted form, and serves as the server-side identifier of the HTML element that can interact with the server data, for example, input, select, textarea, Frame Elements (IFRAME, frame, and window names, used to specify target and button in other frames or Windows, these elements are related to the submission of forms (the Framework element acts on the target of the form). The browser sets the request sent to the server according to the name, and only receives the element with name on the receiving page of the form, therefore, the element assigned with ID cannot receive the value through the form. On the server side, we can use request. Params to obtain the value submitted by the element based on its name. In form, if the name is not specified, it will not be sent to the server.
2) The 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.
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 the name instead, for example: <a name = "pagebottom"> </a>, for example, <strong> <a name = "1" id = "1"> </a> experience (XP) </strong>. For details, see the example.
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.
5) when associating IMG and map elements, 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 ).
6) attributes of certain elements, such as attribute, Meta, And Param. For example, define <Param name = "appletparameter" value = "value"> for an object or <meta name = "author" content = "Dave raggett"> in Meta.
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. There is another problem with the name attribute. When we dynamically create elements that can contain the name attribute, we cannot simply use the value assignment element. name = "... "to add its name, but must use document. createelement ('<element name = "myname"> </element>') adds the name attribute to the element. What does this mean? You can see the following example.
<Script language = "JavaScript">
VaR input = Document. createelement ('input ');
Input. ID = 'myid ';
Input. Name = 'myname ';
Alert (input. outerhtml );
</SCRIPT>
The result displayed in the message box is: <input id = myid>
<Script language = "JavaScript">
VaR input = Document. createelement ('<input name = "myname"> ');
Input. ID = 'myid ';
Alert (input. outerhtml );
</SCRIPT>
The result displayed in the message box is: <input id = myid name = myname>
Differences between name and ID
The ID must comply with the logo requirements, such as case sensitivity. It is best not to include underscores (because CSS is not compatible ). There are basically no requirements for name, and even numbers can be used. Table, TR, TD, Div, P, span, H1, Li, and other elements generally use ID. The element related to the form can also be assigned an id value, but the method for referencing these elements when assigning an id value to these elements is changed as follows:
When name is assigned, the reference element is used: Document. formname. inputname or document. Frames ("framename ")
When an ID is assigned, the reference element is used: Document. All. inputid or document. All. frameid.
Except for the elements related to the form, only the ID can be assigned and the name cannot be assigned, these elements include body, Li, A, table, TR, TD, Th, P, Div, span, pre, DL, DT, DD, Font, and B.
Difference between ID and class attributes in CSS
1) class is a class that sets the label and is used to specify the style of the element. In the CSS style, use the lower-case"Point"And".In the HTML page, class = "css5" is used to select the call. The named CSS is also called the CSS selector.
For example:. css5 {attribute: attribute value;} the selector calls "<Div class =" css5 "> I am a class example </div> in HTML.
. Baobao {color: lime; Background: # ff80c0}
Usage: class = "Baobao"
2) ID is the identifier of the tag. Defines a unique style of an element. When defining a CSS style,#".
For example, a CSS rule:
# Binbin {font-size: larger}
Usage: Id = "Binbin"
ID is a tag used to distinguish between different structures and content, just like your name. If two people in a room have the same name, they will be confused;
3) The ID Usage in CSS is the same as that in class, but the class is changed to ID. For example:
Define the ID in the CSS style
# Css5 {Height: 25px; width: 200px ;}
Call ID:
<Div id = "css5"> ID Example </div>
A class is a style that can be embedded in any structure or content, just like a dress.
4) The concept is different.
ID is to first find the structure/content, and then define the style; class is to first define a style, and then set to multiple structures/content.
(1) 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.
(2) 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, a header <masthead>, 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.
In practical application, the class may be useful for text layout, and the ID is useful for macro layout and design of various elements.
<ID is unique, while class is a class, applicable to containers that can be used multiple times>
Precautions for ID and name
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.
What if n (n> 1) HTML element IDs are the same on the page? How can we reference them in DHTML objects?
If we use the ASPX page, this situation is not easy, because the Asp.net 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.
But if it is not a dynamic page, we have to repeat the ID. What should I do with IE?
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 when the HTML render contains 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, And the subscript of the array indicates the order in which IDs appear.
Reference recommendations:
ID name class differences
Browser kernel Introduction
Introduction to basic JS knowledge
A collection of code that is frequently used for Web pages.
JSP page access user verification