当前位置:首页 > JavaScript

js实现tab跳转

2026-04-07 10:49:56JavaScript

实现Tab跳转的基本思路

使用JavaScript监听键盘事件,当按下Tab键时,将焦点切换到下一个可聚焦元素。常见的可聚焦元素包括<input><select><textarea>和带有tabindex属性的元素。

document.addEventListener('keydown', function(event) {
    if (event.key === 'Tab') {
        event.preventDefault();
        const focusableElements = document.querySelectorAll('input, select, textarea, button, [tabindex]:not([tabindex="-1"])');
        const currentElement = document.activeElement;
        let currentIndex = Array.from(focusableElements).indexOf(currentElement);

        if (currentIndex === -1) {
            focusableElements[0].focus();
        } else {
            const nextIndex = (currentIndex + 1) % focusableElements.length;
            focusableElements[nextIndex].focus();
        }
    }
});

自定义Tab顺序

通过tabindex属性可以自定义元素的Tab顺序。tabindex="0"表示元素可聚焦且按自然顺序排列,tabindex="-1"表示元素不可通过Tab键聚焦。

<input type="text" tabindex="1">
<button tabindex="3">Button</button>
<select tabindex="2">
    <option>Option 1</option>
</select>

处理动态内容

对于动态添加的元素,需要重新获取可聚焦元素列表。可以使用MutationObserver监听DOM变化。

js实现tab跳转

const observer = new MutationObserver(function() {
    focusableElements = document.querySelectorAll('input, select, textarea, button, [tabindex]:not([tabindex="-1"])');
});
observer.observe(document.body, { childList: true, subtree: true });

循环跳转

默认情况下,Tab键会在页面末尾停止。通过以下代码可以实现循环跳转:

const focusableElements = document.querySelectorAll('input, select, textarea, button, [tabindex]:not([tabindex="-1"])');
focusableElements[0].focus();

document.addEventListener('keydown', function(event) {
    if (event.key === 'Tab') {
        event.preventDefault();
        const currentElement = document.activeElement;
        let currentIndex = Array.from(focusableElements).indexOf(currentElement);
        const nextIndex = (currentIndex + 1) % focusableElements.length;
        focusableElements[nextIndex].focus();
    }
});

反向Tab跳转(Shift+Tab)

通过检查event.shiftKey可以实现反向跳转。

js实现tab跳转

document.addEventListener('keydown', function(event) {
    if (event.key === 'Tab') {
        event.preventDefault();
        const focusableElements = document.querySelectorAll('input, select, textarea, button, [tabindex]:not([tabindex="-1"])');
        const currentElement = document.activeElement;
        let currentIndex = Array.from(focusableElements).indexOf(currentElement);

        if (event.shiftKey) {
            const prevIndex = (currentIndex - 1 + focusableElements.length) % focusableElements.length;
            focusableElements[prevIndex].focus();
        } else {
            const nextIndex = (currentIndex + 1) % focusableElements.length;
            focusableElements[nextIndex].focus();
        }
    }
});

兼容性处理

确保代码在旧版浏览器中正常工作,可以使用event.keyCodeevent.which作为备用方案。

document.addEventListener('keydown', function(event) {
    const key = event.key || event.keyCode;
    if (key === 'Tab' || key === 9) {
        event.preventDefault();
        // 跳转逻辑
    }
});

禁用特定元素的Tab跳转

通过设置tabindex="-1"或动态移除tabindex属性可以禁用某些元素的Tab跳转。

document.getElementById('no-tab').setAttribute('tabindex', '-1');

标签: 跳转js
分享给朋友:

相关文章

js实现验证码

js实现验证码

使用Canvas生成图形验证码 在HTML中创建一个Canvas元素用于绘制验证码。通过JavaScript随机生成数字或字母组合,并添加干扰线、噪点等干扰元素增强安全性。 <canvas i…

js树实现

js树实现

树的基本概念 树是一种非线性的数据结构,由节点和边组成。每个节点包含一个值和指向子节点的引用。树的顶部节点称为根节点,没有子节点的节点称为叶节点。 树的实现方式 在JavaScript中,树可以通过…

实现js页面跳转

实现js页面跳转

使用 window.location.href 通过修改 window.location.href 属性实现跳转,这是最常用的方法: window.location.href = "https://…

js验证码的实现

js验证码的实现

验证码的基本实现原理 验证码(CAPTCHA)的核心目标是区分人类用户和自动化程序。JavaScript可用于生成或验证客户端验证码,但需注意纯前端验证可能被绕过,通常需结合后端验证。 纯前端验证码…

js分页实现

js分页实现

分页的基本原理 分页的核心是通过计算当前页码和数据偏移量,从服务器或本地数据中截取对应范围的数据进行展示。通常需要以下参数:当前页码(currentPage)、每页条数(pageSize)、总数据量(…

js实现定位

js实现定位

使用Geolocation API获取当前位置 在JavaScript中,可以通过浏览器内置的Geolocation API获取用户的地理位置信息。该API需要用户授权才能访问位置数据。 if (n…