我可以在 HTML 元素的 name 属性中使用空格吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5688200/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 08:03:22  来源:igfitidea点击:

Can I use white spaces in the name attribute of an HTML element?

html

提问by Tchypp

A question has rised in my project team as we are designing a web page. Can we use white characters (like space) in the name attribute of an HTML element ? eg. <input type="checkbox" name="first check box">

在我们设计网页时,我的项目团队提出了一个问题。我们可以在 HTML 元素的 name 属性中使用白色字符(如空格)吗?例如。<input type="checkbox" name="first check box">

My concern is mainly the behavior of different browsers with such an attribute value.

我关心的主要是具有这样一个属性值的不同浏览器的行为。

We are now in the design phase, until we get to write some code and test this, a long time will pass, so I am asking you experts about this.

我们现在处于设计阶段,直到我们开始编写一些代码并对此进行测试,时间会过去很长时间,所以我请教各位专家。

Thank you !

谢谢 !

回答by Quentin

Yes.

是的。

The name attributecontains CDATA. It can be more or less anything you like. (You shouldn't include leading or tailing white space because user agents can ignore it, but white space in the middle is fine).

name属性包含CDATA。它可以或多或少是你喜欢的任何东西。(您不应该包含前导或尾随空格,因为用户代理可以忽略它,但中间的空格很好)。

Note that nameattributes do not have NAME tokens as their data type (although the idattribute does (the idattribute doesn't take an ID token)), so you don't have the restrictions imposed on NAME tokens.

请注意,name属性没有 NAME 标记作为其数据类型(尽管id属性有(id属性不接受 ID 标记)),因此您没有对 NAME 标记施加的限制。

回答by Mikecito

Most browsers will handle this fine, as long as they are properly quoted. However, for sake of eliminating corner cases, it's much wiser to user CamelCase and avoid the probability of browsers not handling it correctly.

大多数浏览器都会处理这个问题,只要它们被正确引用。但是,为了消除极端情况,使用 CamelCase 并避免浏览器无法正确处理它的可能性要明智得多。