Home  >  Article  >  Web Front-end  >  Detailed introduction to the difference between the ID and Name attributes of HTML elements

Detailed introduction to the difference between the ID and Name attributes of HTML elements

高洛峰
高洛峰Original
2017-03-20 16:53:341475browse

It can be said that almost everyone who has done web development has asked, what is the difference between the ID and Name of an element? Why do we need Name when we have ID?! 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 obviously unique, and Name is repeatable.

Last week I also encountered the problem of ID and Name. I entered an input type="hidden" on the page and only wrote an ID='SliceInfo'. After assigning the value, submit and use Request in the background. Params["SliceInfo"] cannot get the value Detailed introduction to the difference between the ID and Name attributes of HTML elements. Later, I suddenly realized that it should be marked with Name, so I added Name='SliceInfo' to the input, and everything was ok.

The answer to ID and Name in the first paragraph 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 complicated, because Name has many uses, so it cannot be completely replaced by ID, thus canceling it. 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'Group, 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 It is implemented based on the same Name attribute.
Purpose 3: Establish the anchor point in the page. We know that 91d83b4822db6c9c4aabe5918601cc24link5db79b134e9f6b82c0b36e0489ee08ed is to obtain a page hyperlink. If the href attribute is not used, instead Using Name, such as: 0540000f1e2e8c1414f3fa2eece38c9e5db79b134e9f6b82c0b36e0489ee08ed, we get a page anchor.
Purpose 4: As the Identity of the 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 associating between the IMG element and the MAP element, if you want to define the hotspot area of ​​the IMG, you need to use its attribute usemap, so usemap="#name" (associated MAP Name of the element).
Use 6: Attributes of certain specific elements, such as attribute, meta and param. For example, define parameters c482d017ac37d0493b1edf90b225c0b5 for Object or db0f0129396a875c0f18375780d74f03 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. .

Of course, the Name attribute of the HTML element can also play a role as an ID in the page, because in the DHTML object tree, we can use document.getElementsByName to obtain an object array containing all specified Name elements in the page. . There is another problem with the Name attribute. When we dynamically create an element that can contain the Name attribute, we cannot simply use the assignment element.name = "..." to add its Name. We must use document.createElement( when creating the Element. 'dc549ff4942b59c926c9e4e7a7132efea24c0203f0ae689239f065103120aae7') adds the Name attribute to the element. what does this mean? Just look at the example below to understand.

<script language="JavaScript">
var input = document.createElement(&#39;INPUT&#39;);
input.id = &#39;myId&#39;;
input.name = &#39;myName&#39;;
alert(input.outerHTML);
</script>

The result displayed in the message box is: 7c4910ae3e1ff2731c00ced15cbfeb3c.

<script language="JavaScript">
var input = document.createElement(&#39;<INPUT name="myName">&#39;);
input.id = &#39;myId&#39;;
alert(input.outerHTML);
</script>

    消息框里显示的结果是:fcd57d8d6086a1073f231e2d47bdc82a。
    初始化Name属性的这个设计不是IE的缺陷,因为MSDN里说了要这么做的,可是这样设计的原理什么呢?我暂时没有想太明白Detailed introduction to the difference between the ID and Name attributes of HTML elements

    这里再顺便说一下,要是页面中有n(n>1)个HTML元素的ID都相同了怎么办?在DHTML对象中怎么引用他们呢?如果我们使用ASPX页面,这样的情况是不容易发生的,因为aspnet进程在处理aspx页面时根本就不允许有ID非唯一,这是页面会被抛出异常而不能被正常的render。要是不是动态页面,我们硬要让ID重复那IE怎么搞呢?这个时候我们还是可以继续使用document.getElementById获取对象,只不过我们只能获取ID重复的那些对象中在HTML Render时第一个出现的对象。而这时重复的ID会在引用时自动变成一个数组,ID重复的元素按Render的顺序依次存在于数组中。

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

Statement:
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