哪个更“语义 HTML”?对于错误消息?

发布于 2024-11-07 14:44:23 字数 135 浏览 2 评论 0原文

我们正在与一位同事讨论并尝试决定使用哪个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

我也只是我 2024-11-14 14:44:23

我相信您应该使用 直接将错误消息与输入元素关联起来。

引用 W3 规范

LABEL 元素可用于将信息附加到控件。

通过 for 属性创建多个引用,可以将多个 LABEL 与同一控件关联。


另请参阅错误消息:与 <标签>

I believe you should use a <label> which directly associates the error message with the input element.

quoting the W3 specs

The LABEL element may be used to attach information to controls.

and

More than one LABEL may be associated with the same control by creating multiple references via the for attribute.


See also Error Message: <span> vs <label>

时光沙漏 2024-11-14 14:44:23

WCAG2.0 指南,关于

1.3.1 信息和关系: 信息通过演示传达的结构、结构和关系可以通过编程方式确定或以文本形式提供。

列出足够的技术。

G138:在使用颜色提示时使用语义标记

H49:使用语义标记来标记强调或特殊文本

基于这些,我推断唯一适合错误的标签是 ;


使用 还不够,因为它显示了标签内容和目标字段之间的关系,但没有传达内容的重要性。

WCAG2.0 guidelines, on

1.3.1 Info and Relationships: Information, structure, and relationships conveyed through presentation can be programmatically determined or are available in text.

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.

追星践月 2024-11-14 14:44:23

原则上,元素的选择应该由含义决定,而不是由“如何以及在哪里显示”它(如 @Babiker 建议)。这就是整个想法,更不用说该选择将对(例如)视力障碍用户产生的影响(对于他们来说,“显示它的位置”可能完全丢失)。

不幸的是,即使 HTML 5 也没有这样的元素。也许是“旁边”(http://www.w3.org/ TR/html5/sections.html#the-aside-element) 是最接近的吗?该规范在第 4.3.5 节中将其描述为:

aside 元素表示页面的一部分,该部分由与side 元素周围的内容相关的内容组成,并且可以被视为与该内容分离。这些部分通常在印刷版式中表示为侧边栏。

该元素可用于印刷效果,例如引文或侧边栏、广告、导航元素组以及被视为与页面主要内容分开的其他内容。

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:

The aside element represents a section of a page that consists of content that is tangentially related to the content around the aside element, and which could be considered separate from that content. Such sections are often represented as sidebars in printed typography.

The element can be used for typographical effects like pull quotes or sidebars, for advertising, for groups of nav elements, and for other content that is considered separate from the main content of the page.

尐籹人 2024-11-14 14:44:23

没有正确的标签可用于错误消息。这完全取决于您想要显示错误的方式和位置。一旦您决定了这些事情,您的选择就会缩小,因为标签属性和限制有所不同。但是

是怎么来的呢?

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?

薄荷梦 2024-11-14 14:44:23

只是扔进罐子里:

    -Elements 怎么样。如果输入字段的验证由于多种原因而失败,那么您可能需要向该字段附加多条错误消息。

文件上传字段示例:

  • 您尝试上传的文件格式错误。 (仅允许 pnggifjpg
  • 您尝试上传的文件太大。 (最大 1MB)
  • 等等...

例如,Zend-Framework 错误装饰器正在使用 ul-Elements。

但是,如果我必须在 divpspan 之间进行选择,我的选择将是 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 file you tried to upload has the wrong format. (Only png, gif and jpg are allowed)
  • The file you tried to upload is to large. (Max 1MB)
  • and so on...

The Zend-Framework Error-Decorators for example are using ul-Elements.

However if I had to choose, between div, p and span, my choice would be div. Best stylable (Background-color for example).

输什么也不输骨气 2024-11-14 14:44:23

我在2024年使用的是

    tag with some aria atributes. Here is the typical I use inside a form or its fieldset for each control on it:

        <div>
            <input aria-invalid="true" aria-errormessage="MyNumberField-Errors" id="MyNumberField" type="number" value="15" max="10" placeholder="Ammount" />
            <label for="MyNumberField">Ammount</label>
            <ul id="MyNumberField-Errors" role="alert">
                <li>The ammount can not be bigger than 10.</li>
            </ul>
        </div>
        
    
  • 我使用
      因为它可以一次列出不同的错误。
  • 我使用 aria-invalid 将控件内容标记为无效。
  • 我使用 aria-errormessage 在输入控件中设置包含错误消息的标签的 id(在我的例子中为 ul 标签)。
  • 我将 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:

        <div>
            <input aria-invalid="true" aria-errormessage="MyNumberField-Errors" id="MyNumberField" type="number" value="15" max="10" placeholder="Ammount" />
            <label for="MyNumberField">Ammount</label>
            <ul id="MyNumberField-Errors" role="alert">
                <li>The ammount can not be bigger than 10.</li>
            </ul>
        </div>
        
    
  • I use <ul> because it can be a list of different errors at a time.
  • I use aria-invalid to mark the control content as invalid.
  • I use aria-errormessage to set in the input control the id of the tag containing the error message (in my case, the ul tag).
  • I use 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.

抹茶夏天i‖ 2024-11-14 14:44:23

你可以使用

<pre>Error</pre>

You could use

<pre>Error</pre>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文