|
|
本帖最后由 S8F8ry 于 2026-11-5 20:24 编辑
这个其实应该是网页的开发者设计和考虑的。或者你可以试试使用下面这个油猴脚本:- // ==UserScript==
- // @name 文本框 Tab 切换
- // @version 0.1.0
- // @description 使用 Tab / Shift+Tab 在文本框之间跳转,跳过链接和按钮
- // @author dragonish
- // @namespace https://github.com/dragonish
- // @license GNU General Public License v3.0 or later
- // @match *://*/*
- // @grant none
- // @run-at document-idle
- // ==/UserScript==
- (function () {
- const EXCLUDE_TYPES = new Set(["hidden", "checkbox", "radio", "button", "submit", "reset", "file", "range", "color", "image"]);
- function isTextbox(el) {
- if (!el || !(el instanceof HTMLElement)) return false;
- if (el instanceof HTMLInputElement || el instanceof HTMLTextAreaElement) {
- if (el.disabled) return false;
- }
- if (el.isContentEditable) return true;
- if (el instanceof HTMLTextAreaElement) return true;
- if (el instanceof HTMLInputElement) {
- return !EXCLUDE_TYPES.has((el.type || "text").toLowerCase());
- }
- return false;
- }
- function isVisible(el) {
- if (el.getClientRects().length === 0) return false;
- const cs = getComputedStyle(el);
- return cs.display !== "none" && cs.visibility !== "hidden";
- }
- function getTextboxes() {
- return Array.from(document.querySelectorAll('input, textarea, [contenteditable="true"]')).filter(el => isTextbox(el) && isVisible(el));
- }
- document.addEventListener("keydown", e => {
- if (e.key !== "Tab" || e.ctrlKey || e.metaKey || e.altKey) return;
- const active = document.activeElement;
- if (!isTextbox(active)) return;
- e.preventDefault();
- e.stopPropagation();
- const boxes = getTextboxes();
- const idx = boxes.indexOf(active);
- if (idx === -1) return;
- const next = e.shiftKey ? (idx - 1 + boxes.length) % boxes.length : (idx + 1) % boxes.length;
- boxes[next]?.focus();
- }, true);
- })();
复制代码
|
|