Thuộc tính tabindex kiểm soát thứ tự nhận focus khi người dùng nhấn phím Tab. Giúp cải thiện accessibility (khả năng tiếp cận) cho người dùng bàn phím.
Ví dụ minh họa
Thứ tự tab: Current Affairs (1) → Coding Ground (2) → Quantitative Aptitude (3) → Verbal Ability (4) – khác thứ tự HTML:
<!DOCTYPE html> <html> <body> <a href="https://www.tutorialspoint.com/current_affairs/index.htm" tabindex="1">Current Affairs</a><br> <a href="https://www.tutorialspoint.com/codingground.htm" tabindex="2">Coding Ground</a><br> <a href="https://www.tutorialspoint.com/quantitative_aptitude/index.htm" tabindex="3">Quantitative Aptitude</a><br> <a href="https://www.tutorialspoint.com/verbal_ability/index.htm" tabindex="4">Verbal Ability</a><br> <p>Thử nhấn Tab trên bàn phím để xem thứ tự focus thay đổi.</p> </body> </html>
Giá trị tabindex
| Giá trị | Hành vi |
|---|---|
0 | Phần tử vào tab order tự nhiên (theo thứ tự DOM). Có thể focus bằng JS. |
> 0 | Thứ tự ưu tiên (1 trước, 2 sau...). Không khuyến khích – làm rối tab order tự nhiên. |
-1 | Không vào tab order, nhưng có thể focus bằng JS (element.focus()). Dùng cho modal, dropdown. |