Home > Web Front-end > HTML Tutorial > The specific difference between HTML element ID and Name attributes

The specific difference between HTML element ID and Name attributes

零下一度
Release: 2017-05-13 15:05:39
Original
1727 people have browsed it

The difference between the ID and Name attributes of HTML elements

I suddenly got interested today and wanted to delve into the specific differences between these two attributes
The most classical answer: ID is like a person ID number, and Name is like his name. ID is obviously unique, but Name can be repeated. ​​Obviously, the answer to ID and Name is too general. Of course, that explanation is completely correct for ID, which is the Identity of the HTML element on the client side. Name is actually much more complicated, because Name has many uses, so it cannot be completely replaced by ID, thus canceling it. The specific uses are:
Use 1: As a server-side indicator of HTML elements that can interact with the server, such as input, select, textarea, and button. We can get the value submitted by the element through Request.Params based on its Name on the server side.

Use 2: HTML element Input type='radio' grouping, we know that the radio button control is in the same grouping class, the check operation is mutex, only one radio can be selected at the same time, this grouping is based on the same Name attribute to achieve.

Purpose 3: Establish an anchor point in the page. We know that link is to obtain a page hyperlink. If you do not use the href attribute and use Name instead, For example: , we get a page anchor. ​

​ Purpose 4: Identity as an object, such as Applet, Object, Embed and other elements. For example, in an Applet object instance, we will use its Name to refer to the object.

Purpose 5: When linking IMG elements and MAP elements, if you want to define the hotspot area of ​​IMG, you need to use its attribute usemap, so usemap="#name" (Name of the associated MAP element) ). ​

​Use 6: Attributes of certain specific elements, such as attribute, meta and param. For example, define parameters for Object or in Meta.

Obviously these uses cannot be simply replaced by ID, so the difference between ID and Name of HTML elements is not the difference between ID number and name. They have different functions. .

<input    name= "xx ">     会提交数据
<input   id= "xx ">     不会提交数据 
     <input   id= "xx "   name= "yy ">
<label   for= "xx "> Label </label>     这里的for属性必须是指向一个id,否则没效果 
     <input   name= "xx "   type= "radio "   value= "1 ">
<input   name= "xx "   type= "radio "   value= "2 ">
<input   name= "xx "   type= "radio "   value= "3 "> 这里必须name相同才能成为一组单选按钮,而id则没有这个功能。 
     <input   name= "xx ">     对应脚本document.getElementsByName( "xx ")[0]
<input   id= "xx ">         对应脚本document.getElementByIdx_x( "xx ")
Copy after login

The above is the specific difference between these two attributes.

【Related Recommendations】

1. Special Recommendation:"php Programmer Toolbox" V0.1 version Download

2. Free html online video tutorial

3. php.cn original html5 video tutorial

The above is the detailed content of The specific difference between HTML element ID and Name attributes. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template