名称属性在HTML文档中必须是唯一的吗? [英] Does a name attribute have to be unique in a HTML document?

查看:844
本文介绍了名称属性在HTML文档中必须是唯一的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我记得在规范中读过一次, id 属性和名称属性共享相同的名称空间,必须是独一无二的。从今以后,我一直试图在我的应用程序中满足这个要求,甚至为了给同样的 id name 同样的元素。

但是最近我开始使用ASP.NET MVC 3,并且它(如PHP)可以使用相同的名称属性在几个输入控件上形成服务器端的值集合。我试图查找规范中的相关部分 - 但未能找到它。也许我误解了一些东西,或者读了错误的文档?



那怎么样?我想生成尽可能有效的HTML(在不同的应用程序中都是4.01和5)。我可以毫无惧色地使用这个技巧吗?或者我是否会违反某些规定,并应该坚持唯一的值?

属性仅对< form> 和表单元素(< input> < textarea> < select> )。它用于指定名称与表单提交的名称/值对相关联。



例如:

 < input type =checkboxname =foovalue =1/> 

如果选中,将提交 foo = 1 。在DOM中,通过指定 name 作为索引,可以从 form.elements 集合中引用表单元素。如果 name 不是唯一的,则集合返回一个元素数组而不是元素。现代DOM支持按名称查找表单元素:

  document.getElementsByName(nameValue)


id 属性来自XML世界,并且是任何节点的唯一标识符,而不仅仅是表单元素。与名称属性不同,它在任何HTML节点上都有效。也像名称属性一样,它必须遵循有效的标识符规则。标识应该以alpha开头,并且只包含alpha( [a-zA-Z] ),数字,连字符,下划线和冒号(注意:ASP.NET打破这个规则通过使用下划线开始保留的ID - 因此它们总是会失败一个HTML / XML lint - 实际上一些代理将它们剥离)。通过 id 可以找到任何HTML元素:

  document.getElementById (idvalue)

这只返回一个DOM节点。


I remember reading in the spec once that both the id attribute and the name attribute share the same namespace and have to be unique. Henceforth I've always tried to fulfill this requirement in my applications, dreading even to give the same id and name to the same element.

But lately I've started working with ASP.NET MVC 3, and it (like PHP) can use the same name attribute on several input controls to form a collection of values at server-side. I tried to look up the relevant section in the spec - but failed to find it. Perhaps I have misunderstood something then, or read the wrong documentation?

How is it then? I want to produce as valid HTML as possible (both 4.01 and 5 in different apps). Can I use this trick without fear? Or would I be violating something and should better stick to unique values?

解决方案

The name attribute is only valid on the <form> and form elements (<input>,<textarea> and <select>). It's used to specify the name to associate with the name/value pair that is submitted on a form post.

For example:

<input type="checkbox" name="foo" value="1" />

if checked will submit foo=1. In the DOM you can reference form elements from the form.elements collection by specifying the name as the index. If name is not unique, the collection returns an array of elements rather than the element. Modern DOM's support looking up form elements by name as:

 document.getElementsByName(nameValue)

note: it always returns an array even if only one element is found.

id attribute is from the XML world, and is a unique id for any node, not just form elements. Unlike the name attribute it is valid on any HTML node. Also like the name attribute, it must follow the valid identifier rules. The identified should start with an alpha, and only contain alpha ([a-zA-Z]), numbers, hyphen, underscore and colons (note ASP.NET breaks this rule by starting reserved IDs with a underscore - thus they will always fail an HTML/XML lint - actually some proxies strip them). To find any HTML element by id you use:

document.getElementById(idvalue)

this only returns one DOM node.

这篇关于名称属性在HTML文档中必须是唯一的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆