OnClick 和 OnKeyPress 符合标准

发布于 2024-09-28 22:16:18 字数 376 浏览 0 评论 0原文

我正在开发一个大型网站(1600 多个页面),需要升级才能通过标准合规性。因此,对于每个 OnClick,标准都必须有一个 OnKeyPress 处理程序,以便不使用鼠标的人仍然可以访问内容。

有些标签有一个 onclick javascript 处理程序。 EG:

<a onclick="doSumat();">

以下是跨浏览器、有效的 javascript:

<a onclick="doSumat();" onkeypress="this.onclick();" >

它在所有浏览器中都能可靠地工作吗?对于使用屏幕阅读器的人来说,结果是否与单击鼠标相同?

I am working on a large website (1600+ pages) that needs upgrading to pass standards compliance. As a result, for every OnClick there has to be, say the Standards, an OnKeyPress handler, so that people not using a mouse can still access the content.

Some tags have an onclick javascript handler. EG:

<a onclick="doSumat();">

Is the following cross browser, working javascript:

<a onclick="doSumat();" onkeypress="this.onclick();" >

Will it work reliably in all browsers and will the result be the same as a mouse click for someone using a screen reader?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故人爱我别走 2024-10-05 22:16:18

W3C - SCR35:通过使用锚点和按钮的 onclick 事件使操作键盘可访问。

虽然“onclick”听起来像是与鼠标相关,但 onclick 事件实际上映射到链接或按钮的默认操作。当用户用鼠标单击元素时,会发生默认操作,但当用户聚焦元素并按 Enter 或空格键时,以及通过辅助功能 API 触发元素时,也会发生默认操作。

来源http://www.w3.org/TR/WCAG20-TECHS/SCR35

W3C - SCR35: Making actions keyboard accessible by using the onclick event of anchors and buttons.

While "onclick" sounds like it is tied to the mouse, the onclick event is actually mapped to the default action of a link or button. The default action occurs when the user clicks the element with a mouse, but it also occurs when the user focuses the element and hits enter or space, and when the element is triggered via the accessibility API.

Source http://www.w3.org/TR/WCAG20-TECHS/SCR35

山有枢 2024-10-05 22:16:18

在这种情况下,为什么不使用 onclick 和 onkeypress 处理程序调用 doSumat(); 呢?

如果您需要 this 上下文,那么您需要使用 doSumat.call(this);,但您仍然可以将其放置在两个处理程序中。

<a onclick="doSumat();" onkeypress="doSumat();">

对于 1600 个页面,我想您正在尝试找到一些简单的东西来使用 onclick 处理程序附加到每个元素,但定义规则来盲目复制 < 中的所有内容不应该那么复杂。 code>onclick 语句到 onkeypress 表达式。

In this case, why not call doSumat(); with both the onclick and onkeypress handlers?

If you need a this context, then you'll need to use doSumat.call(this);, but you can still place that in both handlers.

<a onclick="doSumat();" onkeypress="doSumat();">

With 1600 pages, I imagine you're trying to find something simple to append to every element with an onclick handler, but shouldn't be that much more complex to define rules to blindly copy everything from an onclick statement to an onkeypress expression.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文