简体   繁体   English

标签与输入无关

[英]Label not associated to input

Using the built in Chrome Lighthouse to run accessibility audit on a WP site showed the signup field as being invalid due to no associated label. 使用内置的Chrome Lighthouse在WP站点上运行辅助功能审核,显示由于没有关联的标签,注册字段无效。 I've run into this before and I can't understand why it doesn't think it's associated. 我之前遇到过这种情况,我无法理解为什么它不认为它是相关的。 I can get it to pass by adding an aria-label tag to the input directly but I shouldn't have to do that. 我可以通过直接在input添加aria-label标签来传递它,但我不应该这样做。

Here is a code snippet that is created from Subscribe2 这是一个从Subscribe2创建的代码片段

<label for="s2email">Your email:</label>
<br>
<input type="text" name="email" id="s2email" value="Enter email address..." size="20" onfocus="if (this.value === 'Enter email address...') {this.value = '';}" onblur="if (this.value === '') {this.value = 'Enter email address...';}">

Can be found here: https://blog.collaborative.org/ 可以在这里找到: https//blog.collaborative.org/

When you look at the code sample in isolation, it looks fine: it has a label explicitly associated with the input element using the for and id attributes. 当你单独查看代码示例时,它看起来很好:它有一个使用forid属性与input元素显式关联的label

However, when you inspect the site, it looks different: due to the style rule .s2_form_widget label { display: none; } 但是,当您检查站点时,它看起来不同:由于样式规则.s2_form_widget label { display: none; } .s2_form_widget label { display: none; } the label is invisible. .s2_form_widget label { display: none; }标签是不可见的。 (You can see this when you use the Inspector in Firefox, move to the label in the DOM tree and check the associated styles.) (当您在Firefox中使用Inspector时,可以看到这一点,移动到DOM树中的标签并检查相关的样式。)

Note that display: none does not only hide the label visually but also for screen readers. 请注意, display: none不仅可以在视觉上隐藏标签,还可以为屏幕阅读器隐藏。 As a result, the input element has no label from the point of view of a screen reader or another assistive technology and fails the requirement that form controls need explicit labels. 因此,从屏幕阅读器或其他辅助技术的角度来看,输入元素没有标签,并且不能满足表单控件需要显式标签的要求。 (As an alternative, you might try hiding the label off-screen with a negative margin.) (作为替代方案,您可以尝试在屏幕外隐藏带有负边距的标签。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM