Conversion: differences between html id and name

Source: Internet
Author: User

ID and name are basically the same as tags. name is an old method, and ID is invented Based on name. It is more extensive than name "modernization. <a...> the original name (when it was just issued) is used for turning the page to the previous one. We generally think that ID is like a person's ID card number, which is obviously unique, and name is like his name, which can be repeated.

ID and name: enter an input type = "hidden" on the page, write only one id = 'sliceinfo', assign a value to submit, and use request in the background. params ["sliceinfo"] cannot be found. Later, I understood the cause and used name to mark it. 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. Specific uses include:

Purpose 1: ID of the server as an HTML 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, Meta, And Param. For example, define <Param name = "appletparameter" value = "value"> for an object or <meta name = "author" content = "Dave raggett"> in Meta.

The above functions cannot be 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.
Bytes -----------------------------------------------------------------------------------------
Except the tags of base, Head, HTML, Meta, script, style, and title, IDs are available.

Name is only used for applet, select, form, frame, IFRAME, IMG, A, input, object, MAP, Param, Meta. (name sometimes has its own purpose)

Name is not defined in Table, and Id should be used.

Some labels can have both name and ID.

In an HTML webpage, the ID is unique.

The role of ID in HTML:

(1) Use the ID to select the corresponding style sheet ).
(2) <a ..> destination of The Link
(3) Use the script language to find the destination (find the ID tag)
(4) as the name, used to declare an object element
(5) Identify Peugeot as a tag or block (used to extract data from HTML webpages into a database or translate HTML into other formats, and so on ).

(Refer to the definition in HTML 4.0)

Name was originally used for identification, but we recommend that you use ID to identify elements according to the specifications.
You can only use the following name:
1. Form Control name. The submitted data is controlled by the Control name rather than the ID. Because many names correspond to multiple controls at the same time, for example
Checkbox and radio, and Id must be unique in the entire document. In addition, the browser will set the request sent to the server according to the name. Therefore, if the ID is used, the server does not
To obtain the data.
2. The name of the frame and window, used to specify the target in another frame or window.
You can only use the following ID:
1. association between label and Form Control,
<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.
2. CSS element selection mechanism, specifying the elements of the application style in the form of # myid, cannot be replaced by name.
3. Get objects in the script:
IE supports directly referencing the ID object with ID (rather than name) in the script. For example, to obtain the input content in the script, you can directly
Myinput. value.
If Dom is used, document. getelementbyid ("myinput"). value is used. If name is used, the form containing the control is usually obtained first. For example:
Document. Forms [0], and then reference name from form. Note that the calculated value will be sent to the server.
Other differences between name and ID are:
The ID must comply with the logo requirements, such as case sensitivity. It is best not to include underscores (because CSS is not compatible ). However, there are basically no requirements for name, and even numbers can be used.
.
Use CSS to control the stay style of the link,
You can write # m_blog Div. Opt A: hover {color: # d57813} Or # mylink: hover {color: # d57813}
Name is mainly used in interactive web pages. After a form is submitted to a server-side script, it receives the variable processing capacity. From the perspective of source code standardization and compatibility, such as on the client
To index an object in the script, we recommend that you use the document. getelementbyid () method. Do not directly use the name value.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.