HTML 中的 Name 与 Id 属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4487860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Name vs Id attribute in HTML
提问by DarkLightA
Are there any advantages to using <div id="here" ...
instead of <div name="here" ...
使用<div id="here" ...
代替有什么好处吗<div name="here" ...
are they both referenced to as #here?
他们都被称为#here吗?
回答by Pratik
Here are some difference between both :
以下是两者之间的一些区别:
- name has never been a div element attribute.
- name as used on the form controls (input, textarea, select, button elements) is radically different from the id attribute on named elements. In this case, the name attribute relates to how data is labeled when sent to server, and multiple elements may share the same name. The id attribute on the other hand is for identifying one unique element for the purposes of scripting, styling, or addressing.
The use of the name attribute on other elements than form controls was in HTML4.01 the same as that of id, but it allowed for a wider set of characters than the id attribute and wasn't controlled in quite the same way. Because of this ambiguity the W3C decided to deprecate/remove name attribute on those elements in favor for the unambigous id attribute in XHTML. This is also connected to another detail of XML however - only one attribute of any element may be of the type ID, which would not be the case if they let name stay on the element but corrected the ambiguity problems.
As the name attribute didn't work the same on those two sets of elements, if was best to remove on of them.
- name 从来都不是一个 div 元素属性。
- 表单控件(输入、文本区域、选择、按钮元素)上使用的名称与命名元素上的 id 属性完全不同。在这种情况下, name 属性与发送到服务器时如何标记数据有关,并且多个元素可能共享相同的名称。另一方面,id 属性用于标识一个独特的元素,用于脚本编写、样式设置或寻址。
在 HTML4.01 中,除了表单控件之外的其他元素上 name 属性的使用与 id 相同,但它允许使用比 id 属性更广泛的字符集,并且不受完全相同的控制方式。由于这种歧义,W3C 决定弃用/删除这些元素上的 name 属性,以支持 XHTML 中明确的 id 属性。然而,这也与 XML 的另一个细节有关 - 任何元素的只有一个属性可能是类型 ID,如果他们让名称保留在元素上但纠正了歧义问题,情况就不会如此。
由于 name 属性在这两组元素上的工作方式不同,因此最好删除其中的一个。
In short, for backwards compatibility you should use name and id attribute both, both set to the same value, for all elements except form controls if you use HTML4.01 or XHTML1.0 Transitional. If you use XHTML1.0 Strict or later you should use only id. For form controls you should use name for what you want the form to send to the server and for DOM0 access, and only use id for styling, DOM1-3 access or addressing reasons
简而言之,为了向后兼容,如果您使用 HTML4.01 或 XHTML1.0 Transitional,除了表单控件之外的所有元素都应该使用 name 和 id 属性,两者都设置为相同的值。如果您使用 XHTML1.0 Strict 或更高版本,则应仅使用 id。对于表单控件,您应该使用名称作为您希望表单发送到服务器和 DOM0 访问的名称,并且仅将 id 用于样式、DOM1-3 访问或寻址原因
回答by craftsman
It depends where you are going to use them.
这取决于您将在哪里使用它们。
Usually, id
of an element is unique while multiple elements can share same name
.
通常,id
一个元素是唯一的,而多个元素可以共享相同的name
.
Id is referenced as #here
, name is referenced as [name=here]
.
Id 被引用为#here
,name 被引用为[name=here]
。
回答by Andrew Barber
They are not interchaneable, even if they sometimes appear to be.
它们是不可互换的,即使它们有时看起来是可互换的。
Name should only exist on form input fields. It is what that tag will cause the browser to pass in the submission as the name of the field. As Tomalak noted in a comment, DIV actually does not have a Name attribute.
名称应仅存在于表单输入字段中。这是该标签将导致浏览器将提交作为字段名称传递的内容。正如 Tomalak 在评论中指出的那样,DIV 实际上没有 Name 属性。
ID is the unique identifier for the DOM, to manipulate or refer to the tag; for example, in Javascript.
ID 是 DOM 的唯一标识符,用于操作或引用标签;例如,在 Javascript 中。
回答by Mohamed Kamal
The "id" attribute assigns an identifier to the associated element. This identifier must be unique in the document and can be used to refer to that element.
“id”属性为关联元素分配一个标识符。此标识符在文档中必须是唯一的,并可用于引用该元素。
check the links below
检查下面的链接
回答by Christopher Bradshaw
One thing of importance that hasn't been mentioned in the other answers is that a form with a name attribute gets added as a variable to the document object in javascript. Any form controls with a name inside that form are accessible as child objects on the form element.
在其他答案中没有提到的一件重要的事情是具有 name 属性的表单作为变量添加到 javascript 中的文档对象。任何在该表单内具有名称的表单控件都可以作为表单元素上的子对象访问。
So if, for example, you had some HTML like the following:
因此,例如,如果您有一些如下所示的 HTML:
<form name="myForm">
<input name="myInput" type="text" />
</form>
You could access the form with document.myForm
or access the input element with document.myForm.myInput
.
您可以使用 访问表单document.myForm
或使用 访问输入元素document.myForm.myInput
。
See an example of this idea in action on JSFiddle.
在JSFiddle上查看这个想法的例子。
回答by Display name
The answer is:
答案是:
- the id attributeis to be unique across the entirety of the HTML document, and
- the name attributehas no such requirement.
By convention, the name value is used with forms to help with the processing of forms, particularly with radio buttons. How you use the name attribute depends on your use case. If you need some other tag for your use case, you can of course create a new attribute and use that as you will; pick a name for your attribute that will likely be very unique!
按照惯例,名称值与表单一起使用以帮助处理表单,尤其是单选按钮。您如何使用 name 属性取决于您的用例。如果您的用例需要一些其他标签,您当然可以创建一个新属性并随意使用它;为您的属性选择一个可能非常独特的名称!
回答by raRaRa
What you are thinking about is probably the input tag. It offers the name attribute to identify which input is what when the form is submitted.
您正在考虑的可能是输入标签。它提供了 name 属性来标识提交表单时哪个输入是什么。
id would be only used to style the input, whereas name would not.
id 将仅用于设置输入的样式,而 name 则不会。