[HTML5] Accessibility Implementation for complex component
When you developing a complex component by your own, one thing you cannot ignore is Accessibility.
Checkout this link. It lists all things you need to do regarding to accessibility when implements a complex component.
The tech we are using is: Roving Focus
Take radio group as an example, when doing roving focus, we set current focus element's:
tabindex = "0 " checked="checked"
And set other elements to:
tabindex="-1"
(function() { 'use strict'; // Define values for keycodes var VK_ENTER = 13; var VK_SPACE = 32; var VK_LEFT = 37; var VK_UP = 38; var VK_RIGHT = 39; var VK_DOWN = 40; // Helper function to convert NodeLists to Arrays function slice(nodes) { return Array.prototype.slice.call(nodes); } function RadioGroup(id) { this.el = document.querySelector(id); this.buttons = slice(this.el.querySelectorAll('.radio')); this.focusedIdx = 0; this.focusedButton = this.buttons[this.focusedIdx]; this.el.addEventListener('keydown', this.handleKeyDown.bind(this)); } RadioGroup.prototype.handleKeyDown = function(e) { switch(e.keyCode) { case VK_UP: case VK_LEFT: { e.preventDefault(); // This seems like a good place to do some stuff :) if(this.buttons && this.buttons.length && this.focusedIdx > 0) { this.focusedIdx -= 1; } else if(this.buttons && this.buttons.length && this.focusedIdx === 0) { this.focusedIdx = this.buttons.length - 1; } break; } case VK_DOWN: case VK_RIGHT: { e.preventDefault(); // This seems like a good place to do some stuff :) if(this.buttons && this.buttons.length && this.focusedIdx < this.buttons.length - 1) { this.focusedIdx += 1; } else if(this.buttons && this.buttons.length && this.focusedIdx === this.buttons.length - 1) { this.focusedIdx = 0; } break; } } this.changeFocus(this.focusedIdx); // <-- Hmm, interesting... }; RadioGroup.prototype.changeFocus = function(idx) { // Set the old button to tabindex -1 this.focusedButton.tabIndex = -1; this.focusedButton.removeAttribute('checked'); // Set the new button to tabindex 0 and focus it this.focusedButton = this.buttons[idx]; this.focusedButton.tabIndex = 0; this.focusedButton.focus(); this.focusedButton.setAttribute('checked', 'checked'); }; var group1 = new RadioGroup('#group1'); }());
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <meta name="description" content=""> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="main.css"> </head> <body> <div class="demo"> <h3>Drink Options</h3> <ul id="group1" class="radiogroup"> <li tabindex="0" class="radio" checked> Water </li> <li tabindex="-1" class="radio"> Tea </li> <li tabindex="-1" class="radio"> Coffee </li> <li tabindex="-1" class="radio"> Cola </li> <li tabindex="-1" class="radio"> Ginger Ale </li> </ul> </div> <script src="radiogroup.js"></script> </body> </html>
分类:
HTML 5
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2017-03-08 [React] Recompose: Theme React Components Live with Context
2017-03-08 [Angular] Using directive to create a simple Credit card validator
2017-03-08 [Docker] Container & image (docker run)
2017-03-08 [Django] Building the rest API
2016-03-08 [RxJS] Reactive Programming - New requests from refresh clicks -- merge()
2016-03-08 [RxJS] Starting a Stream with SwitchMap & switchMapTo
2016-03-08 [RxJS] Reactive Programming - Rendering on the DOM with RxJS