哪个更“语义 HTML”?对于错误消息?
我们正在与一位同事讨论并尝试决定使用哪个 HTML 元素来显示表单验证错误消息。
我们中的一个人说我们应该使用 span 或 div,因为它是输入字段的一部分,另一个人说它应该是 ap 元素,因为它是文本。
你们觉得怎么样?
We were discussing with a co-worker and trying to decide on what HTML element to use for a form validation error message.
One of us is saying that we should use a span or a div because it is a part of an input field, and the other is saying that it should be a p element because it is a text.
What do you guys think?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我相信您应该使用
直接将错误消息与输入元素关联起来。
引用 W3 规范
和
另请参阅错误消息:与 <标签>
I believe you should use a
<label>
which directly associates the error message with the input element.quoting the W3 specs
and
See also Error Message: <span> vs <label>
WCAG2.0 指南,关于
列出足够的技术。
G138:在使用颜色提示时使用语义标记
和
H49:使用语义标记来标记强调或特殊文本
基于这些,我推断唯一适合错误的标签是
和
;
使用
还不够,因为它显示了标签内容和目标字段之间的关系,但没有传达内容的重要性。
WCAG2.0 guidelines, on
Lists as sufficient techniques.
G138: Using semantic markup whenever color cues are used
And
H49: Using semantic markup to mark emphasized or special text
Based on those, I infer that the only appropriate tags for errors are
<em>
and<strong>
Using
<label>
in not enough as it shows relationship between the label content and the target field, but doesn't communicate the importance of the content.原则上,元素的选择应该由含义决定,而不是由“如何以及在哪里显示”它(如 @Babiker 建议)。这就是整个想法,更不用说该选择将对(例如)视力障碍用户产生的影响(对于他们来说,“显示它的位置”可能完全丢失)。
不幸的是,即使 HTML 5 也没有这样的元素。也许是“旁边”(http://www.w3.org/ TR/html5/sections.html#the-aside-element) 是最接近的吗?该规范在第 4.3.5 节中将其描述为:
In principle, the choice of element should be dictated by the meaning, not by "how and where you want to display" it (as @Babiker suggested). That's kind of the whole idea, not to mention the effects the choice will have on (for example) visually-impaired users (for whom the "where you display it" may be totally lost).
It does seem unfortunate that even HTML 5 doesn't have an element for this. Perhaps 'aside' (http://www.w3.org/TR/html5/sections.html#the-aside-element) would be the closest? The spec describes it in Section 4.3.5 as:
没有正确的标签可用于错误消息。这完全取决于您想要显示错误的方式和位置。一旦您决定了这些事情,您的选择就会缩小,因为标签属性和限制有所不同。但是
是怎么来的呢?
There is no right tag to use for an error message. It all depends on how and where you want to display the error. Once you decide on these things, your choices will be narrowed, as tag properties and limitations differ. But how did
<p>
come in this?只是扔进罐子里:
-Elements 怎么样。如果输入字段的验证由于多种原因而失败,那么您可能需要向该字段附加多条错误消息。
文件上传字段示例:
例如,Zend-Framework 错误装饰器正在使用
ul
-Elements。但是,如果我必须在
div
、p
和span
之间进行选择,我的选择将是 div。最佳样式(例如背景颜色)。Just throwing into the jar: What about
<ul>
-Elements. If an input-field's validation fails for more than one reason, than you may want to attach more than one error-message to that field.Example for an file-upload-field:
The Zend-Framework Error-Decorators for example are using
ul
-Elements.However if I had to choose, between
div
,p
andspan
, my choice would be div. Best stylable (Background-color for example).我在2024年使用的是
tag with some aria atributes. Here is the typical I use inside a form or its fieldset for each control on it:
因为它可以一次列出不同的错误。
role
与"alert"
值一起使用,以指示它是由于与错误消息交互而产生的新警报信息。与问题无关,但也许您可能会问自己为什么我要在
标签之后编写
标签。
这是为了允许我根据 CSS 的
状态为
提供样式,因为不可能访问“上一个” " 通过 css 标记。
通过这种方式,我可以将
"*"
添加到具有所需属性的输入的标签,或者在禁用输入时为标签提供一些特定的样式。然后,
通过 css 样式直观地显示在
之前,尽管在 html 代码中它位于
之后。
What I am using in 2024 is
tag with some aria atributes. Here is the typical I use inside a form or its fieldset for each control on it:
<ul>
because it can be a list of different errors at a time.aria-invalid
to mark the control content as invalid.aria-errormessage
to set in the input control the id of the tag containing the error message (in my case, the ul tag).role
with the"alert"
value to indicate that it is new alert info dued to interaction, with the error messages.Not related with the question, but perhaps you could be asking yourself why I'm writing the
<label>
tag AFTER the<input>
one.This is to allow me to give style to the
<label>
based on the<input>
status trough CSS, as it would not be possible to access a "previous" tag trough css.Doing that way, i can for example add
"*"
to the label for inputs with required atribute, or give some specific style to label when the input is disabled.The
<label>
is then visualy shown before the<input>
trough css styles, althought it is after it in the html code.你可以使用
You could use