简体   繁体   English

禁用所有可访问性 API(屏幕阅读器)读取的占位符文本

[英]Disable placeholder text to be read by all accessibility API(screen reader)

Some screen readers read placeholder value and some do not.有些屏幕阅读器会读取占位符值,有些则不会。 Is there a way in which reading placeholder value can be disabled for All screen reader(in context with HTML input).有没有一种方法可以为所有屏幕阅读器禁用读取占位符值(在 HTML 输入的上下文中)。 Especially the IOS screen reader reads placeholder value, in that case, the same information repeated twice.特别是 IOS 屏幕阅读器读取占位符值,在这种情况下,相同的信息重复了两次。

What I want is to show placeholders for screen users and hidden labels for screen reader users.我想要的是为屏幕用户显示占位符,为屏幕阅读器用户显示隐藏标签。

Edit: When I asked the question it was the first time working on accessibility, to just fix an issue.编辑:当我问这个问题时,这是第一次研究可访问性,只是为了解决一个问题。 On further exploration, I find out that we can just use a hidden label and associate it with the input element using for the attribute.在进一步探索中,我发现我们可以只使用隐藏的 label 并将其与用于属性的输入元素相关联。 Here the w3 documentation link .这里是 w3 文档链接

According to Understanding Success Criterion 2.5.3: Label in Name根据 理解成功标准2.5.3:名称中的Label

As such, in the absence of any other nearby text string (as described in the preceding list), if an input contains placeholder text, such text may be a candidate for Label in Name.因此,在没有任何其他附近文本字符串的情况下(如前面列表中所述),如果输入包含占位符文本,则此类文本可能是名称中 Label 的候选者。 This is supported both through the accessible name calculation (discussed later) and from the practical sense that where a visible label is not otherwise provided, it is likely that a speech-input user may attempt to use the placeholder text value as a means of interacting with the input .这通过可访问名称计算(稍后讨论)和实际意义上得到支持,即在没有以其他方式提供可见 label 的情况下,语音输入用户可能会尝试使用占位符文本值作为交互方式与输入

As long as you do not provide a visual label, the placeholder text is used by assistive technologies (speech-input for instance).只要您不提供视觉 label,辅助技术(例如语音输入)就会使用占位符文本。 Disabling input-assistance to provide more user experience to output-assistance is counter-productive.禁用输入辅助以向输出辅助提供更多用户体验会适得其反。

You perfectly can use a visual label which emulates the placeholder behaviour when the field is empty (with appropriate precautions to make the fields usable with input assistance once the field is filled).您完全可以使用可视化的 label 来模拟字段为空时的占位符行为(采取适当的预防措施,使字段在字段被填充后可在输入帮助下使用)。

For my requirement as I mentioned in the question对于我在问题中提到的要求

What I want is to show placeholders for screen users and hidden labels for screen reader users.我想要的是为屏幕用户显示占位符,为屏幕阅读器用户显示隐藏标签。

The best way is to add a hidden label and associate input element with attribute to the respective label.最好的方法是添加一个隐藏的 label 并将输入元素与属性关联到相应的 label。

<label for="search" class="visuallyhidden">Search: </label>
<input type="text" name="search" id="search" placeholder>
<button type="submit">Search</button>
.visuallyhidden {
      border: 0;
      clip: rect(0 0 0 0);
      height: 1px;
      margin: -1px;
      overflow: hidden;
      padding: 0;
      position: absolute;
      width: 1px;
    }

Also, you can simply hide the label applying style as display: none .此外,您可以简单地隐藏 label 应用样式为display: none

<label style={{display: 'none'}} for="search"> Search: </label> 

You can follow w3.org document .您可以关注w3.org 文档 It's really worth looking at the documentation.真的很值得看一下文档。

If you really want to get creative, you could in theory use JS to manage the placeholder attribute(s).如果你真的想发挥创意,理论上你可以使用 JS 来管理占位符属性。 It would slow down the loading of the page somewhat, but you could then remove the placeholder attribute whenever the input receives focus and re-add it when the input loses focus.它会稍微减慢页面的加载速度,但是您可以在输入获得焦点时删除占位符属性,并在输入失去焦点时重新添加它。 That would have the effect of disabling the placeholder attribute for assistive technology (at least in interactive forms mode).这将具有禁用辅助技术的占位符属性的效果(至少在交互式 forms 模式下)。 Sighted visitors probably wouldn't notice the difference.有视力的游客可能不会注意到差异。 Here's an example in a fiddle .是 fiddle 中的一个示例 I tested in NVDA/Chrome and it worked, but I can't speak for any other screen-reader/browser combinations.我在 NVDA/Chrome 中进行了测试,它工作正常,但我不能说任何其他屏幕阅读器/浏览器组合。

// on document load, add the placeholder text
document.addEventListener("DOMContentLoaded", function(){
   myInput.setAttribute("placeholder", myPlaceholder)
});

// when receiving focus, remove the placeholder text
myInput.addEventListener('focus', (event) => {
    myInput.removeAttribute("placeholder");
});

// when losing focus, re-add the placeholder text
myInput.addEventListener('blur', (event) => {
   myInput.setAttribute("placeholder", myPlaceholder)
});

As others have stated, using placeholders is not a suitable replacement for using labels.正如其他人所说,使用占位符不是使用标签的合适替代品。 If you really don't like the look of labels, you can position them off-screen or use the clip technique and continue using placeholders.如果您真的不喜欢标签的外观,您可以 在屏幕外将它们 position 或使用剪辑技术并继续使用占位符。 The downside is that some screen readers may read both the label and the placeholder, which isn't ideal, but being overly descriptive is better than being under-descriptive.缺点是一些屏幕阅读器可能会同时阅读 label 和占位符,这并不理想,但过度描述总比描述不足好。 I would personally be okay with some things being read twice if it only occurs on some browser/screen-reader combinations, and my code is otherwise valid and meets WCAG requirements.如果某些内容仅出现在某些浏览器/屏幕阅读器组合上,我个人会同意阅读两次,并且我的代码在其他方面有效并符合 WCAG 要求。

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

相关问题 将句子“-”替换为使用屏幕阅读器阅读“破折号”以获得可访问性 - Replace sentence "-" to read "dash" with screen reader for accessibility 屏幕阅读器可访问类似密码的文本 - Screen reader accessibility for password-like text 在 Google Chrome 中,NVDA(辅助功能屏幕阅读器)不会在 Bootstrap 模态对话框中读取文本 - In Google Chrome, NVDA (Accessibility Screen Reader) Does Not Read Text in Bootstrap Modal Dialog 在屏幕阅读器中暂停以获得可访问性 - Pausing in a screen reader for accessibility 屏幕阅读器占位符问题 - Screen Reader Placeholder Issue 如何以屏幕阅读器仍会阅读文本并向用户指示已禁用的方式“禁用”单选按钮? - How to “disable” a radio button in a way that a screen reader would still read the text and indicate to a user that it is disabled? 我可以删除屏幕阅读器提示并保留占位符文本吗 - Can I remove screen reader hint and retain placeholder text 如何限制屏幕阅读器不阅读某些文本? - How to restrict screen reader to not read a certain text? Web 辅助功能屏幕阅读器未读取 JavaScript 弹出对话框 - JavaScript Pop-Up Dialog Not Being Read by Web Accessibility Screen Reader 辅助功能-制表符与屏幕阅读器不匹配 - Accessibility - Tabbing doesn't match the screen reader
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM