简体   繁体   English

以编程方式设置 tabindex

[英]Setting tabindex programmatically

I'm using jQuery and a smooth scrolling code snippet from css-tricks, but for some reason setting tabindex for accessibility isn't working.我正在使用 jQuery 和来自 css-tricks 的平滑滚动代码片段,但出于某种原因,为可访问性设置 tabindex 不起作用。 I've copied the snippet directly, so I'm not sure if there's something else in the implementation that's gone awry or if it's the browser (latest version of Chrome).我已经直接复制了代码片段,所以我不确定在实现中是否还有其他地方出错了,或者它是否是浏览器(最新版本的 Chrome)。 An extra set of eyes on this would be very appreciated!对此有额外的关注将不胜感激!

Original snippet原始片段

Codepen demonstrating the issue Codepen 演示了这个问题

Expected behaviour: When you click on the 'Skip to content' link, it should smooth scroll down to the content div.预期行为:当您单击“跳到内容”链接时,它应该平滑向下滚动到内容 div。 When tabbing with the keyboard afterwards, it should start focusing the links directly after the content div (Test links 4+)之后使用键盘进行 tab 键时,它应该直接在内容 div 之后开始聚焦链接(测试链接 4+)

What's happening: The codepen scrolls down to the content div, but when tabbing with the keyboard, it jumps back up to Test Link 1.发生了什么: codepen 向下滚动到内容 div,但是当使用键盘进行 tab 键时,它会跳回 Test Link 1。

If I add tabindex to the div directly, the expected functionality works: <div id="primary" tabindex="-1">This works</div>如果我直接将 tabindex 添加到 div,预期的功能将起作用: <div id="primary" tabindex="-1">This works</div>

But if I add tabindex programmatically using the smooth scroll snippet, it doesn't.但是,如果我使用平滑滚动代码段以编程方式添加 tabindex,则不会。 I've also tried using.setAttribute() and.prop() instead of.attr(), without luck.我也尝试过使用 .setAttribute() 和 .prop() 而不是 .attr(),但没有成功。

This appears to be to do with the version of jQuery in use.这似乎与正在使用的 jQuery 版本有关。

I was playing about on CodePen, including on the pen by Chris Coyier linked to in the CSS tricks article you got the code from, where everything seems to work as intended.我在 CodePen 上玩,包括在 CSS 技巧文章中链接到的 Chris Coyier 的笔上,您从中获得了代码,一切似乎都按预期工作。 I decided to edit your post to include a code snippet with the code copied from your Codepen, to avoid the need to go offsite to look into it - but abandoned when I realised that, contrary to the Codepen, it appared to work fine.我决定编辑您的帖子以包含一个代码片段,其中包含从您的 Codepen 复制的代码,以避免需要到 go 异地查看它 - 但当我意识到与 Codepen 相反,它似乎工作正常时就放弃了。

But Stack Overflow only lets you add jQuery versions up to 3.3.1, whereas you were using 3.6.0 (the latest) in your codepen.但是 Stack Overflow 只允许您将 jQuery 个版本添加到 3.3.1,而您在代码笔中使用的是 3.6.0(最新)。 And I discovered that changing the version of jQuery in use there altered whether it worked or not - specifically, any version before 3.6.0 (although I only tested 3.x versions), seemed to work fine.而且我发现,更改那里使用的 jQuery 的版本会改变它是否有效——具体来说,3.6.0 之前的任何版本(尽管我只测试了 3.x 版本)似乎都可以正常工作。

So TLDR: this seems to be a bug in the latest jQuery. For your purposes you can presumably just use a slightly older version like 3.5.0.所以 TLDR:这似乎是最新 jQuery 中的一个错误。出于您的目的,您大概可以使用稍旧的版本,如 3.5.0。 I don't know if this has been reported as an issue, nor do I fully know what's happened - but reading the release notes I see they have fixed a bug around focus, it is possible that in so doing they caused this new issue.我不知道这是否被报告为一个问题,我也不完全知道发生了什么 - 但阅读发行说明我看到他们修复了一个关于焦点的错误,他们这样做可能会导致这个新问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM