html - 可访问性:放置 tabindex=-1 以避免重复链接的更好位置?
问题描述
这个问题是关于可访问性的。
这是我的代码:
<a href="https://example.com/url-to-details">
<img src="https://example.com/item.png" alt="some description">
</a>
<a href="https://example.com/url-to-details">some description</a>
这并不完美,因为我们知道我们应该避免相邻链接转到相同的 URL(这是 WAVE 辅助工具在我的网页上对我说的关于这段代码的内容)。换句话说,这里的问题是您因此使用了 Tab 键并且仍然出现在同一个链接上。这并不完美。
我的解决方案是为其中一个链接设置tabindex="-1"。
所以,我的问题是:
1.这是一个好主意,还是你有更好的方法?
2. 从可访问性的角度来看,哪个代码更好:
<a href="https://example.com/url-to-details" tabindex="-1">
<img src="https://example.com/item.png" alt="some description">
</a>
<a href="https://example.com/url-to-details">some description</a>
或者
<a href="https://example.com/url-to-details">
<img src="https://example.com/item.png" alt="some description">
</a>
<a href="https://example.com/url-to-details" tabindex="-1">some description</a>
PS有第三种方法:将两个合二为一<a></a><a></a>
,例如<a> picture + some description</a>
,但由于某些原因我会避免它。
PPS 描述文本“一些描述”对于图像描述和锚标记中的文本都是相同的。
解决方案
我没有看到同时具有使用相同 URL 的图像链接和相邻文本链接的用例。它应该是单个链接,因此您有三个选项:
- 摆脱图片链接,
- 摆脱文本链接,
将图像和文本组合成一个链接,其中图像具有空
alt
属性:<a href="https://example.com/url-to-details"><img src="https://example.com/item.png" alt=""> some description</a>
在第三种情况下,该alt
属性应为空以避免重复文本(屏幕阅读器用户不希望听到两次链接文本)。这也导致更简单的代码不依赖于tabindex="-1"
. 另请参阅WCAG 技术 H2:为同一资源组合相邻的图像和文本链接。
请注意,使用两个相邻的链接,两者都具有href
属性,其中一个具有tabindex=-1
,如问题中所建议的那样,将导致两个链接都列在屏幕阅读器的链接列表中。应该避免这种重复。
推荐阅读
- node.js - 为什么环回给我一个空的 ctx.options 操作挂钩?
- android - 使用 url_launcher 或 flutter_webview_plugin 打开 mp3(音频)网址时没有搜索栏/洗涤器
- javascript - 用户点击链接后,如何禁用链接(preventDefault())10 秒?
- django - Django 翻译用户权限
- angularjs - Angular JS ui-select 指令
- highcharts - Highcharts气泡水平堆叠
- sql - SQL Server Management Studio - n 列、n 行的 UNPIVOT 查询
- kotlin - 如何使用 mockk 模拟新对象
- laravel - Laravel 5.7 和 ExtJS 5 应用配置
- php - 如何从 MikrotikAPI PHP 发布回声